2017-01-06 23 views
3

因此,我幫助某人推出了網站,並且當有人購買特定產品時,他們希望獲得折扣產品。我找到了解決方案並實施了該解決方案,並且在該網站的推出過程中發揮了作用,並且不再在購買產品時改變客戶的角色。我試圖從Woothemes獲得支持,他們不支持定製,並且希望他們購買129美元的擴展來處理這個問題。訂單狀態完成後,更改特定產品購買時的用戶角色

有沒有人有這方面的解決方案仍然有效?

這裏是我的代碼:

// Update User on purchase https://gist.github.com/troydean/9322593 
function lgbk_add_member($order_id) { 

    $order = new WC_Order($order_id); 
    $items = $order->get_items(); 

    foreach ($items as $item) { 
     $product_name = $item['name']; 
     $product_id = $item['product_id']; 
     $product_variation_id = $item['variation_id']; 
    } 

    if ($order->user_id > 0 && $product_id == '247' || $order->user_id > 0 && $product_id == '255') { 
     update_user_meta($order->user_id, 'paying_customer', 1); 
     $user = new WP_User($order->user_id); 

     // Remove role 
     $user->remove_role('customer'); 

     // Add role 
     $user->add_role('author'); 
    } 
} 
add_action('woocommerce_order_status_completed', 'lgbk_add_member'); 

回答

3

UPDATE

通常這個更新的代碼版本應與woocommerce_order_status_completed工作,那麼你應該前嘗試這種代碼
(此代碼也與下一個即將推出的主要WooCommerce更新2.7兼容)。

下面是代碼:

add_action('woocommerce_order_status_completed', 'custom_action_on_completed_customer_email_notification'); 
function custom_action_on_completed_customer_email_notification($order_id) { 

    // Set HERE your targetted products IDs: 
    $targetted_products = array(247, 255); 

    $order = wc_get_order($order_id); 

    if ($order->get_user_id() > 0) { 
     foreach ($order->get_items() as $order_item) { 
      // Here we detect if the a target product is part of this order items 
      if (in_array($order_item['product_id'], $targetted_products)){ 

       // I think tha this is not really needed as it's set when an order has been paid… 
       update_user_meta($order->get_user_id(), 'paying_customer', 1); // 1 => true 

       // Remove all roles and set 'editor' as user role (for current user) 
       $user = new WP_User($order->get_user_id()); 
       $user->set_role('author'); 

       // Product is found, we break the loop… 
       break; 
      } 
     } 
    } 
} 

但因爲我不知道您的訂單是如何改變'completed'狀態,如果你想確保(在所有可能的情況下),客戶會向你買2名的某一款產品,都會有他的角色從'customer'改爲'author'時訂單狀態設置爲'completed',我會建議你試圖使用這個電子郵件通知掛鉤(如果第一個代碼段不起作用)。

例如,在這裏我使用woocommerce_email_before_order_table鉤子,將在一些條件的幫助下執行並觸發「完成的訂單客戶電子郵件通知」。
(此代碼也與下一個即將推出的主要WooCommerce更新2.7兼容)。

這是你重新審視和測試代碼:

add_action('woocommerce_email_before_order_table', 'custom_action_on_completed_customer_email_notification', 10, 4); 
function custom_action_on_completed_customer_email_notification($order, $sent_to_admin, $plain_text, $email) { 

    if('customer_completed_order' == $email->id){ 

     // Set HERE your targetted products IDs: 
     $targetted_products = array(247, 255); 

     if ($order->get_user_id() > 0) { 
      foreach ($order->get_items() as $order_item) { 
       // Here we detect if the a target product is part of this order items 
       if (in_array($order_item['product_id'], $targetted_products)){ 

        // I think tha this is not really needed as it's set when an order has been paid… 
        update_user_meta($order->get_user_id(), 'paying_customer', 1); // 1 => true 

        // Remove all roles and set 'editor' as user role (for current user) 
        $user = new WP_User($order->get_user_id()); 
        $user->set_role('author'); 

        // Product is found, we break the loop… 
        break; 
       } 
      } 
     } 
    } 
} 

代碼放在您的活動子主題(或主題)的function.php文件。或者也可以在任何插件php文件中使用。

+0

非常感謝您的幫助! –

相關問題