簡體   English   中英

Woocommerce 某些產品類別和排除類別的最低訂購量

[英]Woocommerce minimum order for certain product categories and exclude categories

我至少訂購了 3 種冷藏類別的產品。

如果購物車中沒有冷藏類別產品,並且購物車中沒有其他產品類別(捆綁常溫禮品)的任何組合,客戶應該可以結帳。

如果購物車中冷藏類產品少於 3 件,購物車中還有捆綁類產品,客戶應該仍然可以結賬。

我的wc_add_notice error正在正確計算冷藏邏輯,但即使購物車中沒有來自冷藏類別的商品,它也會顯示。

請有人可以幫助我出錯的地方嗎?

// Set minimum quantity per product before checking out
add_action( 'woocommerce_check_cart_items', 'chilled_set_min_total' );
    function chilled_set_min_total() {
        // Only run in the Cart or Checkout pages
        if( is_cart() || is_checkout() ) {

            global $woocommerce, $product;
            $i=0;
          
            // Set minimum product cart total
            //And initialise product catergory counters
            $minimum_chilled = 3;
            $total_chilled = 0;
            $cat_in_cart = false;
            
            //check if chilled product in cart. If exists add to quantity
            foreach ( $woocommerce->cart->cart_contents as $product ) :
                if ( has_term( 'chilled', 'product_cat', $product['product_id'] ) ) {
                   $total_chilled += $product['quantity'];
                }
                if ( has_term( 'bundles', 'product_cat', $cart_item['product_id'] ) ) {
                    $cat_in_cart = true;
                break;
             }
            endforeach;
          //if chilled items is less than minimum display error
            if ($total_chilled < $minimum_chilled) {
                wc_add_notice( sprintf( '<strong>A Minimum of %s products are required from The CHILLED category before checking out.</strong>'
                . '<br />Current number of Chilled items in the cart: %s.',
                    $minimum_chilled,
                    $total_chilled ),
                'error' );
            }
        }
    }

你這一行的問題

如果($total_chilled < $minimum_chilled){

如果我們沒有冷藏,那么 $total_chilled 不會改變並且 = 0

所以我們有 0 < 3 並且它總是 = true

要解決此問題,您需要再添加一項檢查

  //Check if there are any products from this category in the cart
  if ($total_chilled > 0) {
  
  //if chilled items is less than minimum display error
    if ($total_chilled < $minimum_chilled) {
        wc_add_notice( sprintf( '<strong>A Minimum of %s products are required from The CHILLED category before checking out.</strong>'
        . '<br />Current number of Chilled items in the cart: %s.',
            $minimum_chilled,
            $total_chilled ),
        'error' );
    }
    
    }

更新,如果您需要禁用結帳按鈕添加

remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20);

  //Check if there are any products from this category in the cart
  if ($total_chilled > 0) {
  
  //if chilled items is less than minimum display error
    if ($total_chilled < $minimum_chilled) {
        wc_add_notice( sprintf( '<strong>A Minimum of %s products are required from The CHILLED category before checking out.</strong>'
        . '<br />Current number of Chilled items in the cart: %s.',
            $minimum_chilled,
            $total_chilled ),
        'error' );

    remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );

    }
    
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM