2017-10-17 39 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? –