除了 WooCommerce 中的几种特定产品外,最小购物车数量

我在我的 php 中使用以下代码“除 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' );

    }

}


阿波罗的战车
浏览 99回答 1
1回答

侃侃无极

您通过在每一行上使用相同的命名约定来覆盖您的变量。所以如果你想将它应用于多个 ID,你必须将它们放在一个数组中。function min_cart_amount() {&nbsp; &nbsp; ## ----- EXCLUDES A PRODUCT FROM MINIMUM ORDER DOLLAR VALUE Your Settings below ----- ##&nbsp; &nbsp; $min_cart_amount&nbsp; &nbsp;= 130; // Minimum cart amount&nbsp; &nbsp; $except_product_id = array ( 2649, 2659, 1747 ); // Except for this product ID&nbsp; &nbsp; // Loop though cart items searching for the defined product&nbsp; &nbsp; foreach( WC()->cart->get_cart() as $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; // Product id&nbsp; &nbsp; &nbsp; &nbsp; $product_id = $cart_item['product_id'];&nbsp; &nbsp; &nbsp; &nbsp; // Exit if the defined product is in cart&nbsp; &nbsp; &nbsp; &nbsp; if ( in_array( $product_id, $except_product_id) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if( WC()->cart->subtotal < $min_cart_amount ) {&nbsp; &nbsp; &nbsp; &nbsp; wc_add_notice( sprintf(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __( "<strong>A Minimum of %s is required before checking out.</strong><br>The current cart's total is %s" ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_price( $min_cart_amount ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_price( WC()->cart->subtotal )&nbsp; &nbsp; &nbsp; &nbsp; ), 'error' );&nbsp; &nbsp; }}add_action( 'woocommerce_check_cart_items', 'min_cart_amount', 10, 0 );
打开App,查看更多内容
随时随地看视频慕课网APP