2017-04-08 9 views
3

私は電子商取引のウェブサイトを作成しています。私のテーマのfunctions.phpファイルに何を追加して、顧客が店舗から1つの製品のみを購入し、その後1週間他の購入を無効にできるようにするには?WooCommerceで週に1回の購入のみを許可する

お客様は任意の製品を購入することができますが、購入後はさらに1週間分の購入はできません。顧客は1週間後にのみ他の購入を行うことができます。

私はこのコードを使用した製品のために作られた購入を無効にできます。

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

@Abhi私は自分の答えを更新しました... – LoicTheAztec

関連する問題