首页 > 解决方案 > 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' );
        }
    }
}

标签: phpwordpresswoocommercecarthook-woocommerce

解决方案


更新 - 以下代码将避免在定义的最小购物车金额下结帐,但定义的产品 ID 除外:

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

    $min_amount = 15; // Minimum cart amount
    $except_ids = array(37, 53); // Except for this product(s) ID(s)

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

    if( WC()->cart->subtotal < $min_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_amount ),
            wc_price( WC()->cart->subtotal )
        ), 'error' );
    }
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。

在此处输入图像描述


推荐阅读