php - add capabilitie to customer based on product categorie -
my website using wordpress, "the events calendar" plugin , "woocommerce" , manage roles , capabilities "advanced access manager" plugin.
i try add function add capability role of buyer based on product category have bought.
so try product (just bought) or event category user id, going wrong , it's impossible test step step, because process need finish let function work.
here code:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); function custom_process_order($order_id) { $order = new wc_order( $order_id ); $myuser_id = (int)$order->user_id; $user_info = get_userdata($myuser_id); $items = $order->get_items(); // need test : $beta= str_replace( ':', '', tribe_get_event_categories($event->id, array( 'echo' => false, 'label' => '', ' label_before' => '', 'label_after' => '', 'wrap_before' => '', 'wrap_after' => '', ))); $beta = strip_tags($beta); foreach ($items $item) { //check if user have capabilitie if ( ! user_can( $user_id, $capability )){ //add capability specific user $user = new wp_user( $user_id ); $user->add_cap( $beta ); } } return $order_id; }
so example customer buy product category "course_paris_night" , should have role capability "course_paris_night". capability customer access tutorial on same category example.
thanks on code or on way test ! (need learn !)
so !!! 24 april...few days later :
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); function custom_process_order($order_id) { global $current_user; global $wp_roles; $order = new wc_order($item->id); $order->populate($item); $items = $order->get_items(); foreach ($items $key => $item2 ) : $event_id_in_product = absint($item2['product_id']); endforeach; // trouve la catégorie du produit acheté par le stagiaire // terms associated post in woocommerce's taxonomy 'product_cat' $terms = get_the_terms( $event_id_in_product, 'product_cat' ); //get array of ids $term_ids = wp_list_pluck($terms,'term_id'); //get array of parents - 0 not parent $parents = array_filter(wp_list_pluck($terms,'parent')); //get array of ids of terms not parents. $term_ids_not_parents = array_diff($term_ids, $parents); //get corresponding term objects. $terms_not_parents = array_intersect_key($terms, $term_ids_not_parents); //extract name of category array , post it. foreach($terms_not_parents $term_not_parent){ $beta = $term_not_parent->name; $alpha = strip_tags($beta); $alpha = trim(strip_tags($alpha)); $alpha = str_replace(' - ',' ',$alpha); $alpha = strtolower($alpha); $all_roles = $wp_roles->roles; $user = new wp_user( $current_user->id ); $result = $user->add_role($alpha); return $order_id; } }
but don't results. testing again here has user info:
wp_user object ( [data] => stdclass object ( [id] => 50 [user_login] => buyer.new [user_pass] => $p$bklx7.czfvnll43ezazdmo84shzqo41 [user_nicename] => buyer-new [user_email] => mymail@mysite.com [user_url] => [user_registered] => 2016-04-23 22:05:27 [user_activation_key] => [user_status] => 0 [display_name] => new ) [id] => 50 [caps] => array ( [mycourse saint-malo intermédiaire] => 1 [mycourse paris avancé] => 1 ) [cap_key] => lep_capabilities [roles] => array ( [0] => mycourse saint-malo intermédiaire ) [allcaps] => array ( [read] => 1 [level_0] => 1 [read_private_tribe_organizers] => 1 [mycourse saint-malo intermédiaire] => 1 [mycourse paris avancé] => 1 ) [filter] => )
normaly has user buying 2 new course under category "mycourse saint-malo intermédiaire" , "mycourse paris avancé" should have under "[roles] => array" 2 new roles ...but has can see it's not.
my code adding 2 new role [caps] , [allcaps] not [roles] !!!!
i sure close. idea ?
//update//
sorry !!! forgot important steps , discover not working in test code (i test directly on page not in function...will tomorrow...here it's 2.3 am)
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); function custom_process_order($order_id) { global $current_user; global $wp_roles; $all_orders = get_posts( array( 'numberposts' => -1, //'meta_key' => '_customer_user', //'meta_value' => get_current_user_id(), 'post_type' => 'shop_order', 'post_status' => 'completed', ) ); $args = get_posts( array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => get_current_user_id(), 'post_type' => 'shop_order', 'post_status' => 'completed', ) ); if( ! empty($args)){ foreach($args $item) { setup_postdata( $item ); $order = new wc_order($item->id); $order->populate($item); $items = $order->get_items(); foreach ($items $key => $item2 ) : $event_id_in_product = absint($item2['product_id']); endforeach; //trouve la catégorie du produit acheté par le stagiaire //get terms associated post in woocommerce's taxonomy 'product_cat' $terms = get_the_terms( $event_id_in_product, 'product_cat' ); //get array of ids $term_ids = wp_list_pluck($terms,'term_id'); //get array of parents - 0 not parent $parents = array_filter(wp_list_pluck($terms,'parent')); //get array of ids of terms not parents. $term_ids_not_parents = array_diff($term_ids, $parents); //get corresponding term objects. $terms_not_parents = array_intersect_key($terms, $term_ids_not_parents); //extract name of category array , post it. foreach($terms_not_parents $term_not_parent){ $beta = $term_not_parent->name; $alpha = strip_tags($beta); $alpha = trim(strip_tags($alpha)); $alpha = str_replace(' - ',' ',$alpha); $alpha = str_replace('stage','stagiaire',$alpha); $alpha = strtolower($alpha); $all_roles = $wp_roles->roles; $user = new wp_user( $current_user->id ); $result = $user->add_role($alpha, $alpha, array($alpha =>true,)); // array missing in previous version ! return $order_id; } } }
this solution , it's working great under test page but...
// retrieve customer order $blc_args = get_posts( array('numberposts' => -1,'meta_key' =>'_customer_user','meta_value' => get_current_user_id(),'post_type' => 'shop_order','post_status' => 'completed',) ); if( ! empty($blc_args)){ foreach($blc_args $blc_item) { setup_postdata( $item ); $blc_order = new wc_order($blc_item->id); $blc_order->populate($blc_item); $blc_items = $blc_order->get_items(); foreach ($blc_items $blc_key => $blc_item2 ) : //print_r($item2); $event_id_in_product = absint($blc_item2['product_id']); //get terms associated post in woocommerce's taxonomy 'product_cat' $blc_terms = get_the_terms( $event_id_in_product, 'product_cat' ); //echo"</br>"; //echo "event id embed in product bought : " . $event_id_in_product; //echo"</br>"; //get array of ids $blc_term_ids = wp_list_pluck($blc_terms,'term_id'); //get array of parents - 0 not parent $blc_parents = array_filter(wp_list_pluck($blc_terms,'parent')); //get array of ids of terms not parents. $blc_term_ids_not_parents = array_diff($blc_term_ids, $blc_parents); //get corresponding term objects. $blc_terms_not_parents = array_intersect_key($blc_terms, $blc_term_ids_not_parents); //extract name of category array , post it. foreach($blc_terms_not_parents $blc_term_not_parent){ $blc_start = $blc_term_not_parent->name; } if(preg_match("/(ultérieure|cadeau)/i", $blc_start)){ }else{ $beta = utf8_decode($blc_start); $beta = trim(strip_tags($beta)); $beta_without_accent = strtr($beta, utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'), 'aaaaaceeeeiiiinooooouuuuyyaaaaaceeeeiiiinooooouuuuy'); $beta_with_minuscule = strtolower($beta_without_accent); $beta_with_minuscule = str_replace('balade - ','stagiaire_',$beta_with_minuscule); $beta_with_underscore = str_replace(' - ', '_', $beta_with_minuscule); $beta_stagiaire = str_replace('stage','stagiaire',$beta_with_underscore); global $current_user; $user = new wp_user( $current_user->id ); $user->add_role($beta_stagiaire); } endforeach; } }
...yes...but ! when editing , parsing in functions under child theme doesn't it. seems process because last echo appear (sometimes) , send me right "role create".
here same code build include in function.php:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); function custom_process_order($order_id) { global $current_user; $order = wc_get_order( $order_id ); $items = $order->get_items(); // echo"</br>"; // echo "product id : " . ($order->id); //$order->populate($item); //print_r($order); $items = $order->get_items(); foreach ($items $key => $item2 ) : //print_r($item2); $event_id_in_product = absint($item2['product_id']); //get terms associated post in woocommerce's taxonomy 'product_cat' $terms = get_the_terms( $event_id_in_product, 'product_cat' ); //echo"</br>"; //echo "event id embed in product bought : " . $event_id_in_product; //echo"</br>"; //get array of ids $term_ids = wp_list_pluck($terms,'term_id'); //get array of parents - 0 not parent $parents = array_filter(wp_list_pluck($terms,'parent')); //get array of ids of terms not parents. $term_ids_not_parents = array_diff($term_ids, $parents); //get corresponding term objects. $terms_not_parents = array_intersect_key($terms, $term_ids_not_parents); //extract name of category array , post it. foreach($terms_not_parents $term_not_parent){ $start = $term_not_parent->name; } //echo"</br>"; //echo "product category : " . $start; //echo"</br>"; if(preg_match("/(ultérieure|cadeau)/i", $start)){ //echo"</br>"; //echo "don't add product category : " . $start; //echo"</br>"; }else{ $beta = utf8_decode($start); $beta = trim(strip_tags($beta)); $beta_without_accent = strtr($beta, utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'), 'aaaaaceeeeiiiinooooouuuuyyaaaaaceeeeiiiinooooouuuuy'); $beta_with_minuscule = strtolower($beta_without_accent); $beta_with_minuscule = str_replace('balade - ','stagiaire_',$beta_with_minuscule); $beta_with_underscore = str_replace(' - ', '_', $beta_with_minuscule); $beta_stagiaire = str_replace('stage','stagiaire',$beta_with_underscore); echo"</br>"; echo "role create : " . $beta_stagiaire; echo"</br>"; $user = new wp_user( $current_user->id ); $user->add_role($beta_stagiaire); } endforeach; return $order_id; }
and has can imagine test it's not easy because have payment process.
any error let somewhere ? don't see it.
Comments
Post a Comment