2017-04-08 176 views
3

我正在創建一個電子商務網站。我可以在我的主題的functions.php文件中添加什麼,以允許客戶只從商店購買一種產品,然後在1周內禁用任何其他購買?WooCommerce每週只允許購買一次

客戶可以購買任何產品,但購買後,一週內不能再購買任何產品。客戶只能在一週後進行其他任何購買。

我只能禁止使用此代碼對產品進行購買:

add_filter('woocommerce_add_cart_item_data', 'woo_allow_one_item_only'); 

function woo_allow_one_item_only($cart_item_data) { 

global $woocommerce; 
$woocommerce->cart->empty_cart(); 

// Do nothing with the data and return 
return $cart_item_data; 

} 

$customer_orders = get_posts(array(
'numberposts' => -1, 
'meta_key' => '_customer_user', 
'meta_value' => get_current_user_id(), 
'post_type' => wc_get_order_types(), 
'post_status' => array('wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed'), 

)); 

// Order count 
$order_count = 1; 

if (count($customer_orders) >= $order_count) { 
add_filter('woocommerce_is_purchasable', false); 
} 

回答

3

更新:與WC增加兼容性+3

我應該更好地利用迷上自定義函數woocommerce_add_to_cart_validation過濾器鉤將允許產品添加到購物車之前檢查您的條件是否滿足。

這裏是代碼:

add_filter('woocommerce_add_to_cart_validation', 'conditionally_allowing_product_added_to_cart', 10, 3); 
function conditionally_allowing_product_added_to_cart($passed, $product_id, $quantity) { 

    // If cart is not empty, customer will not be allowed and a notice will be displayed 
    if(! WC()->cart->is_empty()){ 
     wc_add_notice('You can only add an item to cart', 'error'); 
     $passed = false; 
     return $passed; // Not Allowed 
    } elseif(! is_user_logged_in()){ // When user is not logged in 
     $passed = true; 
     return $passed; // Allowed 
    } else { 
     $customer_orders = wc_get_orders($args = array(
      'numberposts' => -1, 
      'meta_key' => '_customer_user', 
      'meta_value' => get_current_user_id(), 
      'post_status' => array('wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed'), 
     )); 
     $customer_orders_count = count($customer_orders); 
     if(! empty($customer_orders_count) || $customer_orders_count > 0){ 
      foreach($customer_orders as $customer_order){ 
       // Added compatibility with WC +3 
       $order_date = method_exists($customer_order, 'get_date_created') ? $customer_order->get_date_created() : $customer_order->post->order_date; 
       // Stores each order timestamp in an array 
       $orders_time[] = strtotime($order_date); 
      } 
      // Sorting orders timestamps 
      sort($orders_time); 
      // Get the last timestamp (last date/time) 
      $last_order_time = end($orders_time); 
      $week = 7*24*60*60; // a week in seconds 
      // "Now" timestamp 
      $now_time = strtotime('now'); 
      // Allowed time limit for customer to make a new order 
      $new_order_allowed_time = $last_order_time + $week; 

      if($now_time >= $new_order_allowed_time){ 
       $passed = true; // Allowed 
      } else { 
       // Display a notice when customer is not allowed yet 
       wc_add_notice('You are not allowed yet to add any product in cart', 'error'); 
       $passed = false; // Not Allowed 
      } 
     } 
     return $passed; 
    } 
} 

的代碼放在你的活躍兒童主題(或主題)的function.php文件或也以任何插件文件。

此代碼已經過測試並可正常工作。

+0

我嘗試了一個新用戶,但現在我甚至不能進行我的第一次購買。首次購買時,我收到錯誤「您尚未在購物車中添加任何產品」。請幫忙。謝謝。 – Abhi

+0

@Abhi我忘記了這種情況...我的代碼將被更新。回來5分鐘。你可以用現有的用戶測試它... – LoicTheAztec

+0

@Ahi我已經更新了我的答案... – LoicTheAztec