2016-04-21 47 views
1

我的网站使用wordpress,“活动日历”插件和“woocommerce”,并使用“高级访问管理器”插件管理角色和功能。根据产品分类为客户添加功能

我尝试添加一个函数,该函数将根据他们刚刚购买的产品类别为买方的角色添加功能。

所以我试图让产品(刚买)或用户ID的事件类别,但事情错了,它几乎是不可能的测试一步一步,因为这个过程必须完成,让功能工作。

下面是代码:

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 to test that : 

    $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 as $item) { 

     //check if user already have capabilitie 
     if (! user_can($user_id, $capability)){ 

      //add capability to specific user 
      $user = new WP_User($user_id); 
      $user->add_cap($beta); 
     } 
    } 
    return $order_id; 
} 

因此,例如,一个客户购买产品与类别“course_paris_night”,所以它应有的作用能力“course_paris_night”。凭借此功能,该客户将通过示例访问相同类别的教程。

感谢您对代码或测试方式的任何帮助! (需要学习!)

因此!!! 4月24日...几天后:

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 as $key => $item2) : 
     $event_id_in_product = absint($item2['product_id']); 
    endforeach; 

    // trouve la catégorie du produit acheté par le stagiaire 
    // Get all terms associated with post in woocommerce's taxonomy 'product_cat' 
    $terms = get_the_terms($event_id_in_product, 'product_cat'); 

    //Get an array of their IDs 
    $term_ids = wp_list_pluck($terms,'term_id'); 

    //Get array of parents - 0 is not a parent 
    $parents = array_filter(wp_list_pluck($terms,'parent')); 

    //Get array of IDs of terms which are 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 the name of the category from the array and post it. 
    foreach($terms_not_parents as $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; 
    } 
} 

但我没有得到任何好的结果。因此,测试再这里是我所得到的具有用户信息:

WP_User Object 
(
[data] => stdClass Object 
    (
     [ID] => 50 
     [user_login] => buyer.new 
     [user_pass] => $P$BKlx7.CzfvnLL43EZaZdmo84shZQo41 
     [user_nicename] => buyer-new 
     [user_email] => [email protected] 
     [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有我的用户是买了两个新课程下的类别“mycourse圣马洛intermédiaire”和“mycourse巴黎AVANCE”我应该在“[角色] =>阵列“两个新的角色......但你可以看到它不是。

我的代码将两个新角色添加到[caps]和[allcaps]中,但不添加到[roles]中!

我相信我非常接近。任何想法 ?

// UPDATE //

对不起!我忘了一些重要的步骤,我终于发现什么没有在我的测试代码的工作(我直接测试它的网页无法在功能上...会做,明天...这里是上午02点03分)

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 as $item) { 
    setup_postdata($item); 

$order = new WC_Order($item->ID); 

$order->populate($item);   
$items = $order->get_items(); 

foreach ($items as $key => $item2) : 
    $event_id_in_product = absint($item2['product_id']); 
    endforeach; 

     //trouve la catégorie du produit acheté par le stagiaire 
    //Get all terms associated with post in woocommerce's taxonomy 'product_cat' 
    $terms = get_the_terms($event_id_in_product, 'product_cat'); 

    //Get an array of their IDs 
    $term_ids = wp_list_pluck($terms,'term_id'); 

    //Get array of parents - 0 is not a parent 
    $parents = array_filter(wp_list_pluck($terms,'parent')); 

    //Get array of IDs of terms which are 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 the name of the category from the array and post it. 
    foreach($terms_not_parents as $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 was missing in previous version ! 

return $order_id; 
} 
} 
} 
+0

这对我来说并不容易,完全按照你的要求去做,老实说你 –

+0

这就是我所理解的。客户完成订单后,您希望为他们创建一个登录帐户,并根据所购产品的类别确定其角色。该角色直接与具有相同名称的教程类别相关。 – imvain2

+0

假设你想限制你的教程给购买你的课程的人,但你只希望他们只访问与他们购买的产品类别对应的教程:例如:我购买一门“基础课程”,这样我就可以访问“基本“教程而不是”高级“教程。 (使用插件根据用户角色和功能管理访问)因此,客户被注册为“客户角色”,并且无法访问“基本教程”,因为他需要“基本”功能。 –

回答

0

这是解决方案,它在我的测试页的工作很好,但是......

// 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 as $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 as $blc_key => $blc_item2) : 
    //print_r($item2); 
    $event_id_in_product = absint($blc_item2['product_id']); 

    //Get all terms associated with 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 an array of their IDs 
    $blc_term_ids = wp_list_pluck($blc_terms,'term_id'); 

    //Get array of parents - 0 is not a parent 
    $blc_parents = array_filter(wp_list_pluck($blc_terms,'parent')); 

    //Get array of IDs of terms which are 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 the name of the category from the array and post it. 
    foreach($blc_terms_not_parents as $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; 
} 
} 

...是...但是!当在我的子主题下编辑和解析我的函数时,它不会这样做。有时它似乎处理它,因为最后一个回声出现(有时)并将我发回正确的“创建角色”。

这是相同的代码,但建设是包括在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 as $key => $item2) : 
    //print_r($item2); 
    $event_id_in_product = absint($item2['product_id']); 

    //Get all terms associated with 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 an array of their IDs 
    $term_ids = wp_list_pluck($terms,'term_id'); 

    //Get array of parents - 0 is not a parent 
    $parents = array_filter(wp_list_pluck($terms,'parent')); 

    //Get array of IDs of terms which are 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 the name of the category from the array and post it. 
    foreach($terms_not_parents as $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 that 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 to create : " . $beta_stagiaire; 
    echo"</br>"; 

    $user = new WP_User($current_user->ID); 

    $user->add_role($beta_stagiaire); 
    } 
    endforeach; 


return $order_id; 
} 

并拥有你能想象到测试它不是那么容易,因为我需要做的支付过程。

任何错误我让某个地方?我没看到它。

+0

工作方案:...它不适用于我的孩子主题的功能,所以我把它放在我的孩子主题中的自定义“thankyou.php”中,现在它工作在100%。如果你想为你的应用使用它,只需定制我处理角色和类别名称的方式。该代码不是一个干净的代码,如果有人找到一种方法来清理它,并使其作为功能工作...让我知道。 –

+0

不要忘记将你的php保存为UTF8 NO BOM ;-) –