WooCommerce:仅为特定产品类别设置最低订单金额

时间:2021-02-04 08:41:34

标签: php wordpress woocommerce cart checkout

我需要为 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' 
                    );

                  }

              }
    }
}

参考文献:

有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

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

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' );
            }
        }
    }
}

它应该有效。

答案 1 :(得分:-1)

最后,我发现该解决方案对@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' );

                }

            
         }
    }
}
相关问题