最小购物车金额(WooCommerce中的几种特定产品除外) [英] Minumun cart amount except for several specific products in WooCommerce

查看:65
本文介绍了最小购物车金额(WooCommerce中的几种特定产品除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中使用以下代码"最小购物车数量(Woocommerce中的特定产品除外)"允许超过Woocommerce的最低购物车价值$ 130.

I'm using the following code "Minimum cart amount except for a specific product in Woocommerce" in my php that allows the override of the Woocommerce minimum cart value of $130.

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

Works fine but only for one defined product. In this case product_id 2649

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

I am trying to further modify this code by adding lines of except_product_id... but that does not help.

如何通过产品ID 修改以列出例外列表?

How can I modify to make a list of exceptions by 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' );
    }
}

推荐答案

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

You overwrite your variable by using the same naming convention on every new line. So if you want to apply this for multiple IDs you have to put them in an array.

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

这篇关于最小购物车金额(WooCommerce中的几种特定产品除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆