2017-10-19 76 views

回答

0

您可以使用此代碼

add_action('woocommerce_order_status_completed', 'misha_change_role_on_purchase'); 

function misha_change_role_on_purchase($order_id) { 

// get order object and items 
$order = new WC_Order($order_id); 
$items = $order->get_items(); 

$product_id = 55; // that's a specific product ID 

foreach ($items as $item) { 

     if($product_id == $item['product_id'] && $order->user_id) { 
      $user = new WP_User($order->user_id); 

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

      // Add role 
      $user->add_role('subscriber'); 
     } 

} 

} 

如果你想按類別進行檢查,只需添加一個條件與has_term($category_id, 'product_cat', $item['product_id'])

+0

太好了!非常感謝 –

相關問題