2017-10-17 46 views
-2


購買特定產品註冊時,我有woocommerce商店WordPress站點,我想強迫用戶只購買特定產品時,需要註冊。
我該怎麼做?
謝謝隊WooCommerce

回答

0

這是您的查詢代碼。

add_action('woocommerce_after_checkout_validation' , 'restict_registration_for_some_products', 10, 2); 
function restict_registration_for_some_products($data, $errors) { 

    if(isset($data['createaccount']) && !$data['createaccount']) { 

     $retricted_ids = get_resticted_product_ids(); 

     if(isset($retricted_ids) && $retricted_ids != null) { 

      $cart_content = WC()->cart->get_cart_contents(); 

      $cart_ids = wp_list_pluck($cart_content, 'product_id'); 
      $cart_ids = array_values($cart_ids); 
      $common_ids = array_intersect($retricted_ids, $cart_ids); 

      if(isset($common_ids) && $common_ids != null) { 
       $errors->add('account_registration', __('You are not allowed to purchase these products without creating an account.', 'text-domain')); 
      } 
     } 
    } 
} 
function get_resticted_product_ids() { 
    //specific product ids 
    return array(110,96,70); 
} 
+0

Thanx,但我需要強制用戶只在購買特定產品或類別時註冊。不強制註冊任何產品。 –

+0

你好,我編輯了我的答案,我測試了這個代碼,並且正在完美地工作。 –

+0

我在哪裏輸入產品ID? –