2016-05-17 7 views
1

他の人がこの状況に遭遇していないかどうかを確認するために広範囲に検索しました。Woocommerce:カートに商品/カテゴリがいくつか含まれている場合の最小注文を設定してください

基本的に、私は顧客がカートに追加できる2つのアイテムがあります。小計が15ドル以上でない場合、それらのアイテムのいずれかでチェックアウトできないようにしたいと思います。

IDをコードにドロップするだけで問題ありません。または、私はそれらを同じカテゴリに割り当て、この最小値をカテゴリごとに設定できます。

今まで私が持っていたのは、普遍的な最小値を設定する基本的なPHPです。私はちょうど私のニーズを満たすためにこれを制限する方法を考え出すためにいくつかの助けが必要です

おかげさまでこれをあなたの助けに感謝します。
私は開発者ではないデザイナーですので、非常に感謝しています。唯一のカートおよびチェックアウトページのカート内のいくつかの製品カテゴリまたは製品のIDのための最小値を設定

// Set a minimum dollar amount per order 
add_action('woocommerce_check_cart_items', 'spyr_set_min_total'); 
function spyr_set_min_total() { 
    // Only run in the Cart or Checkout pages 
    if(is_cart() || is_checkout()) { 
     global $woocommerce; 

     // Set minimum cart total 
     $minimum_cart_total = 10; 

     // Total we are going to be using for the Math 
     // This is before taxes and shipping charges 
     $total = WC()->cart->subtotal; 

     // Compare values and add an error is Cart's total 
     // happens to be less than the minimum required before checking out. 
     // Will display a message along the lines of 
     // A Minimum of 10 USD is required before checking out. (Cont. below) 
     // Current cart total: 6 USD 
     if($total <= $minimum_cart_total ) { 
      // Display our error message 
      wc_add_notice(sprintf('<strong>A Minimum of %s %s is required before checking out.</strong>' 
       .'<br />Current cart\'s total: %s %s', 
       $minimum_cart_total, 
       get_option('woocommerce_currency'), 
       $total, 
       get_option('woocommerce_currency')), 
      'error'); 
     } 
    } 
} 

答えて

1

でWooThemesからthisthisで作られたこのテストされていないスニペット、およびthis too

add_action('woocommerce_checkout_process', 'wc_minimum_order_amount'); 
add_action('woocommerce_before_cart' , 'wc_minimum_order_amount'); 

function wc_minimum_order_amount() { 

    // minimum order value 
    $minimum = 10; 

    if (WC()->cart->total < $minimum) { 
     if (sizeof(WC()->cart->get_cart()) > 0) { 
      $products_min = false; 
      foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
       $_product = $values['data']; 
       $_product_id = $_product->id; 
       $terms = get_the_terms($_product_id, 'product_cat'); 
       foreach ($terms as $term) { 
        $_categoryid = $term->term_id; 
       } 
       // your products categories 
       if ($_categoryid === 17 || $_categoryid === 18) { 
        $products_min = true; 

       // your product ID'S 
       } elseif ($_product_id == 64 || $_product_id == 67 || $_product_id == 78) { 
        $products_min = true; 
       } 
      } 
      if((is_cart() || is_checkout()) && $products_min) { 
       wc_print_notice( 
        sprintf('You must have an order with a minimum of %s to place your order, your current order total is %s.' , 
         wc_price($minimum), 
         wc_price(WC()->cart->total) 
        ), 'error' 
       ); 
      } 
     } 
    } 
} 

あなたのカテゴリIDが、あなたのプロダクトIDの最小オーダー値量を設定することができます。

- - U、P D A T E - -

このフックは、メッセージのために働いています。しかし、ユーザがクリックするのを避けるためには、Javascript/jQueryを追加する必要があり、おそらくajax(クライアント側検出)を追加する必要があります。

+0

こんにちは、ありがとうございます。ご返信いただきありがとうございます。 これは素晴らしいスタートですが、残念ながら、ユーザーがCheckoutをクリックしてPayPalを続けるのを実際にブロックしているわけではありません。警告は表示されますが、停止しません。 また、上記の小さな誤字がありました。プロダクトIDが表示される余分な括弧があります。 – Jeff

+0

よろしくお願いいたします。私はちょうどあなたの情報のためにタイプミスがあったことを意味しました。 :)私はそれがまだチェックアウトからユーザーを停止しない場合でも、これを正しいとマークします。私はそれを自分でブロックするためにjQueryを理解できるかどうかを調べるつもりです。ありがとうございました。 – Jeff

+0

あまりにも多くのトラブルがなければ、それは素晴らしいだろう!ここに[カートページへのリンク](http://point5cc.com/cart)があります。これは[特定の最低価格での購入をブロックするアイテム](http://point5cc.com/shop/free-binder/)で、必要に応じて[この製品](http://point5cc.com/shop/anchor-dogtag-silver /)をあなたのカートに入れて最小限にする。ありがとうございました! – Jeff

関連する問題