【问题标题】:WooCommerce: Set minimum order amount for just specific product categoriesWooCommerce:仅为特定产品类别设置最低订单金额
【发布时间】:2021-05-08 13:00:27
【问题描述】:

我需要为 WooCommerce 中的某些类别强制设置最低订单金额,因此在购物车和结帐页面中设置警报。

到目前为止,如果购物车中的总金额低于设置的最低金额,我设法设置了警报,但我无法创建基于类别的过滤器。实际上,购物车中添加了任何其他产品,警报已被超越,并且允许用户购买我想要限制的该类别的产品。

代码如下:

/**
 * Set a minimum order amount for checkout
 */
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_checkout' , 'wc_minimum_order_amount' );
 
function wc_minimum_order_amount() {
    // Set this variable to specify a minimum order value
    $minimum = 30;
    // set array with cat IDs
    $category_ids = array( 336, 427, 433 );
    // set bool that checks is minimum amount has been reached
    $needs_minimum_amount = false; // Initializing

    $subTotal_amount = WC()->cart->subtotal; // Items subtotal including taxes
    $total_amount = WC()->cart->total; // Items subtotal excluding taxes

            if ( $total_amount < $minimum ) { 
              
              // Loop through cart items
              foreach ( WC()->cart->get_cart() as $cart_item ) {
                $product_id   = $cart_item['product_id'];
                $variation_id = $cart_item['variation_id']; 

                // Check for matching product categories
                  if( sizeof($category_ids) > 0 ) {
                      $taxonomy = 'product_cat';
                      if ( has_term( $category_ids, $taxonomy, $product_id ) ) { 
                          $needs_minimum_amount = true;
                          break; // Stop the loop
                      }
                  }
              }

              if( $needs_minimum_amount ) {

                  if( is_cart()) {

                    wc_print_notice( 
                        sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' , 
                            wc_price( WC()->cart->total ), 
                            wc_price( $minimum )
                        ), 'error' 
                    );

                  } else {

                    wc_add_notice( 
                        sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' , 
                            wc_price( WC()->cart->total ), 
                            wc_price( $minimum )
                        ), 'error' 
                    );

                  }

              }
    }
}

参考资料:

有什么帮助吗?

【问题讨论】:

    标签: php wordpress woocommerce cart checkout


    【解决方案1】:

    您的代码中有一些错误……请改用以下内容:

    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_checkout_form', 'wc_minimum_order_amount' );
    function wc_minimum_order_amount() {
        $min_amount = 30; // Min subtotal required
        $taxonomy   = 'product_cat'; // Product category taxonomy | For product tags use 'product_tag'
        $terms      = array( 336, 427, 433 ); // Category terms (can be Ids, slugs, or names)
        $found      = false; // Initializing
    
        $subtotal   = WC()->cart->subtotal; // Incl. taxes
        $total      = WC()->cart->total; // Incl. taxes
    
        if ( $total < $min_amount ) { 
            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $cart_item ) {
                if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ) { 
                    $found = true;
                    break; // Stop the loop
                }
                
                if( $found ) {
                    $message = sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' , 
                        wc_price( $total ), 
                        wc_price( $min_amount )
                    );
                    
                    if( is_cart()) {
                        wc_print_notice( $message, 'error' );
                    } else {
                        wc_add_notice( $message, 'error' );
                    }
                }
            }
        }
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。


    加法:基于具体小计,包括。税费

    此代码版本基于属于特定定义类别的项目小计:

    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_checkout_form', 'wc_minimum_order_amount' );
    function wc_minimum_order_amount() {
        $min_amount = 30; // Min subtotal required
        $taxonomy   = 'product_cat'; // Product category taxonomy | For product tags use 'product_tag'
        $terms      = array( 336, 427, 433 ); // Category terms (can be Ids, slugs, or names)
        
        $subtotal   = 0; // Initializing
    
        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ) {
                $subtotal += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
            }
        }
    
        if ( $subtotal < $min_amount ) {
            if( $found ) {
                $message = sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' ,
                    wc_price( $total ),
                    wc_price( $min_amount )
                );
    
                if( is_cart()) {
                    wc_print_notice( $message, 'error' );
                } else {
                    wc_add_notice( $message, 'error' );
                }
            }
        }
    }
    

    它应该可以工作。

    【讨论】:

    • 你好 LoicTheAztec!首先非常感谢您的回答!不幸的是,代码中仍然存在问题:(实际上将 if 条件放在 foreach 循环中会使函数完全停止工作。相反,将 if 条件放在 foreach 之外会使函数像以前一样工作不好:显示了警报,但它无法识别类别过滤器(继续将其他类别的产品添加到购物车允许用户结帐)!知道出了什么问题吗?
    • @Stefano Oups!我的错!将代码开头的dd_action 替换为add_action ......它应该可以工作。
    • 再次感谢您的宝贵时间!我认为已经真正理解了为什么代码没有按预期运行。变量 $total 考虑购物车中的全部金额,而不管其中包含的产品。因此,即使我想要定位的产品总量(具有特定类别的产品)仍低于 $min 设置的金额,继续添加其他类别的产品也会使警报消失。我需要的是仅检查具有该术语的产品的总量(其 ID 包含在 $terms 变量中)。有什么想法可以实现这个目标吗?
    • @Stefano 我添加了一个代码版本,该代码版本基于属于特定定义类别的项目小计。如果这个答案回答了你的问题,你可以请accept回答,谢谢。
    • 我不得不对您的代码进行一些处理(缺少一些变量/条件),但最终我找到了解决方案!我会写在下面,以防它可以帮助其他人,非常感谢,我真的很感激!
    【解决方案2】:

    最后我发现解决方案在@LoicTheAztec 提供的代码上工作了一点。

    在最终代码下方。经过测试并且可以正常工作。

    // Set a minimum order amount for checkout
    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
    // add_action( 'woocommerce_before_checkout' , 'wc_minimum_order_amount' );
     
    function wc_minimum_order_amount() {
        $min_amount = 30; // Set this variable to specify a minimum order value
        $taxonomies = array('product_cat'); // Product category taxonomy | For product tags use 'product_tag'
        $term_ids = array( 336, 427, 433 ); // Category terms (can be Ids, slugs, or names)
      
        $found = false; // Initializing // set bool that checks if minimum amount has been reached
        
        $subtotal  = 0; // Initializing // set subtotal formed by specific category total (incl. taxes)
        $subTotal_amount = WC()->cart->subtotal; // Items subtotal including taxes
        $total_amount = WC()->cart->total; // Items subtotal including taxes    
    
        // 1 - Loop through cart items targeting those having that specific category
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( has_term( $term_ids, $taxonomies[0], $cart_item['product_id'] ) ) {
                $subtotal += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];                      
            }
        }
    
        // 2 - Check if at least one product of that category is in the cart
        if($subtotal > 0){
          $found = true;
        }
    
        // 2 - check if subtotal is below the min amount set
         if ( $subtotal < $min_amount ) { 
    
                if( $found ) {
    
                    $message = sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' , 
                        wc_price( $subtotal ), 
                        wc_price( $min_amount )
                    );
    
                    if( is_cart()) {
    
                      wc_print_notice( $message, 'error' );
    
                    } else {
    
                      wc_add_notice( $message, 'error' );
    
                    }
    
                
             }
        }
    }
    

    【讨论】:

    • 首先你的问题不够清楚,我的第一个答案是回答你最初的问题并为之努力……然后你改变了最初的问题,你在这里根据我的答案代码添加做出了答案.这不是正确的做事方式,真的很不公平……所以我不会再回答你的问题了。
    猜你喜欢
    • 1970-01-01
    • 2019-07-29
    • 2019-10-07
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2020-05-11
    相关资源
    最近更新 更多