2016-04-21 44 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 ;-) –