最低购物车数量(Woocommerce中特定产品除外)

时间:2018-07-25 06:15:01

标签: php wordpress woocommerce cart hook-woocommerce

在我的网站上,我只允许购买价格最低为15欧元的商品,但希望对一种产品进行例外处理。如果有人知道如何帮助我,我将非常感激。最小订购值的编码如下。有谁知道我可以如何通过产品ID来排除一个产品?

add_action( 'woocommerce_check_cart_items', 'wc_set_min_total' );
function wc_set_min_total() {

    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Setting the minimum cart total
        $minimum_cart_total = 15;

        $total = WC()->cart->subtotal;


        if( $total <= $minimum_cart_total  ) {

            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 个答案:

答案 0 :(得分:0)

以下代码将避免在已定义的最小购物车数量下结帐,但已定义产品ID除外:

add_action( 'woocommerce_check_cart_items', 'min_cart_amount' );
function min_cart_amount() {
    ## ----- Your Settings below ----- ##

    $min_cart_amount   = 15; // Minimum cart amount
    $except_product_id = 37; // Excetpt for this product ID

    // Loop though cart items searching for the defined product
    foreach( WC()->cart->get_cart() as $cart_item ){
        if( $cart_item['data']->get_id() == $except_product_id || $cart_item['product_id'] == $except_product_id )
            return; // Exit if the defined product is in cart
    }

    if( WC()->cart->subtotal < $min_cart_amount ) {
        wc_add_notice( sprintf(
            __( "<strong>A Minimum of %s is required before checking out.</strong><br>The current cart's total is %s" ),
            wc_price( $min_cart_amount ),
            wc_price( WC()->cart->subtotal )
        ), 'error' );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

enter image description here