最小购物车金额(WooCommerce中的几种特定产品除外)

时间:2020-06-04 07:31:13

标签: php wordpress woocommerce product cart

我在PHP中使用以下代码“ Minimum cart amount except for a specific product in Woocommerce”,该代码允许覆盖Woocommerce最低购物车价值$ 130。

工作正常,但仅适用于一种已定义的产品。在这种情况下,product_id 2649

我正在尝试通过添加except_product_id...行来进一步修改此代码 但这无济于事。

如何修改product id的例外列表?

任何帮助表示赞赏

add_action( 'woocommerce_check_cart_items', 'min_cart_amount' );
function min_cart_amount() {
    ## ----- EXCLUDES A PRODUCT FROM MINIMUM ORDER DOLLAR VALUE Your Settings below ----- ##

    $min_cart_amount   = 130; // Minimum cart amount
    $except_product_id = 2649; // Except for this product ID
    $except_product_id = 2659; // Except for this product ID
    $except_product_id = 1747; // Except 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' );
    }
}

1 个答案:

答案 0 :(得分:0)

通过在每个新行上使用相同的命名约定来覆盖变量。因此,如果要将其应用于多个ID,则必须将它们放入数组中。

function min_cart_amount() {
    ## ----- EXCLUDES A PRODUCT FROM MINIMUM ORDER DOLLAR VALUE Your Settings below ----- ##

    $min_cart_amount   = 130; // Minimum cart amount
    $except_product_id = array ( 2649, 2659, 1747 ); // Except for this product ID

    // Loop though cart items searching for the defined product
    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Product id
        $product_id = $cart_item['product_id'];

        // Exit if the defined product is in cart
        if ( in_array( $product_id, $except_product_id) ) {
            return;
        }
    }

    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' );
    }
}
add_action( 'woocommerce_check_cart_items', 'min_cart_amount', 10, 0 );