正在try 将优惠券自动应用到WooCommerce checkout . 我已经try 了一些代码片段,但似乎无法正常工作.

它要么抛出严重错误,要么不可靠地应用优惠券.

我在WooCommerce上创建了以下优惠券:

  • 黑色10:如果客户花费>GB 100&<GB 199.99,购物车可享受10%的折扣
  • BLACK20:如果客户花费>GB 200,购物车可享受20%的折扣

以下是我的相关代码:

add_action( 'woocommerce_checkout_before_order_review' , 'add_coupon_notice' );
function add_coupon_notice() { 
    $cart_total = WC()->cart->get_subtotal();

    $currency_code = get_woocommerce_currency();
    wc_clear_notices();

    if ( $cart_total > 200 ) {
        WC()->cart->remove_coupon( 'black10' );
        WC()->cart->apply_coupon( 'black20' );
        wc_print_notice( '20% off £200 or more - Discount Applied!', 'notice' );
    } elseif ( $cart_total > 100 ) { 
        WC()->cart->remove_coupon( 'black20' );
        WC()->cart->apply_coupon( 'black10' );
        wc_print_notice( '10% off £100 or more - Discount Applied!', 'notice' );
    }
    

    wc_clear_notices();
}

但它的工作并不可靠.

推荐答案

您可以使用以下替换代码,它将根据定义的购物车项目小计including taxes阈值金额可靠地应用/删除优惠券,并显示自定义通知:

add_action( 'woocommerce_before_calculate_totals' , 'discount_based_on_cart_items_subtotal' );
function discount_based_on_cart_items_subtotal( $cart ) { 
    if ( is_admin() && !defined('DOING_AJAX') )
        return;
    
    $subtotal = 0; // Initializing

    // Loop through cart items to get the subtotal excluding taxes
    foreach ( $cart->get_cart() as $item ) {
        $subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
    }
    
    if ( $subtotal >= 100 && $subtotal < 200 ) { 
        if ( ! $cart->has_discount('black10') ) {
            $cart->apply_coupon( 'black10' );
            $notice = __('10% OFF for '.wc_price(100).' or more - Discount Applied!');
        }
        if ( $cart->has_discount('black20') ) {
            $cart->remove_coupon( 'black20' );
        }
    } elseif ( $subtotal >= 200 ) {
        if ( ! $cart->has_discount('black20') ) {
            $cart->apply_coupon( 'black20' );
            $notice = __('20% OFF for '.wc_price(200).' or more - Discount Applied!');
        }
        if ( $cart->has_discount('black10') ) {
            $cart->remove_coupon( 'black10' );
        }
    } else {
        if ( $cart->has_discount('black20') ) {
            $cart->remove_coupon( 'black20' );
        }
        if ( $cart->has_discount('black10') ) {
            $cart->remove_coupon( 'black10' );
        }
        wc_clear_notices();
    }
    
    if ( isset($notice) ) {
        wc_clear_notices();
        wc_add_notice( $notice, 'notice' );
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.


对于小计excluding taxes,替换为:

$subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];

有:

$subtotal += $item['line_subtotal'];

Note:

不会在优惠券设置中设置最低金额,因为代码是处理这一点.

Php相关问答推荐

在Laravel中,如果我用session—put()存储一些信息,客户端浏览器是否可以使用这些信息?>

我如何知道BaseController中当前运行的是哪个控制器?

从WooCommerce购物车和 checkout 页面自定义发货标签

如何修复条带元素中的错误&&客户端_机密&

获取要删除的图像的公共ID

中继器内置中继器的ACF WordPress组

PHP测试字符串的所有POST值

模式 bootstrap 未显示

根据WooCommerce中的自定义用户元数据值更改用户角色

将购物车按钮重定向到Checkout-Wooccommerce

Shopware 6 插件:如何删除/迁移布尔配置到多选配置

PHP - 计算非数值数组

根据WooCommerce中选定的变体重量更改输入数量步值

在 PHP 中验证签名

如何使用动态变量定义 bind_result?

Laravel 查询关系是否为 bool

为什么 ECDSA 384 签名验证在 GO 中失败,但在 PHP 中却没有?

PHP Remedy API 调用以创建附件无效的条目(使用 Postman Works!)

如何在 php/laravel 中获取元素在数组中的位置

使用 v-for 的存储链接中的 Img src 在 Laravel vuejs 中不起作用