php - 除 WooCommerce 中的特定产品外,最低购物车金额

标签 php wordpress woocommerce cart hook-woocommerce

我只允许在我的网站上订购最低值(value)为 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' );
        }
    }
}

最佳答案

已更新 - 以下代码将避免在定义的最小购物车金额下结帐,但定义的产品 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 文件。测试和工作。

enter image description here

关于php - 除 WooCommerce 中的特定产品外,最低购物车金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51511991/

相关文章:

php - 将 javascript 日期字符串解析为时间戳。 tz 信息在日期内!

php - jQuery 提交前确认

mysql - 无法使复杂的 mysql 查询工作

php - NextGEN Gallery 模板加载页面两次。?

wordpress - 用户注册后创建产品

php - 在后台使用 ffmpeg 进行转换

javascript - 如何使用 php time() 作为基准时间在 javascript 中显示世界时间?

javascript - 禁用/删除元素中的默认 !important 声明

php - 在 WooCommerce 中使用自定义添加到购物车 () 添加两个不同的产品

php - WooCommerce 问题 - 仅返回产品尺寸的整数部分