首页 > 解决方案 > 添加优惠券后隐藏和刷新 WooCommerce 结帐

问题描述

我们想在 WooCommerce 结账时隐藏“有优惠券?添加一个...”,如果优惠券已添加,或者当客户在结帐页面上添加优惠券时。

目前,我们在下面使用此代码,当客户在购物车页面上输入优惠券然后导航到结帐页面时,它就可以工作。在这种情况下,“有优惠券?添加一个...”消息不可见。如果购物车页面上没有添加优惠券,则该消息可见。

这工作正常!但是当客户在结帐页面上添加优惠券时,它不起作用。

1.) 我们收到“已添加优惠券”消息,但添加优惠券的消息仍然可见,并且优惠券未在订单表中计算。=> 页面刷新后一切正常。

2.) 当客户在结账时移除优惠券时,我们会收到优惠券已移除的消息,但折扣仍然在订单表中可见。=> 页面刷新后,它会再次显示所有内容。

所以现在我正在尝试在添加或删除优惠券后刷新页面。但是我很难获得正确的事件。我想我们必须通过js来做到这一点?还是有 PHP 方法?

add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );

function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
  
    global $woocommerce;
    
    if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
        return false;
    }
    return $coupons_enabled;
}

标签: wordpresswoocommercecheckout

解决方案


Your code should be like this

add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );

function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
  if(is_checkout()){
    global $woocommerce;
    
    if ( ! empty( $woocommerce->cart->get_applied_coupons() ) ) {
        $coupons_enabled = false;
    }
    }
    return $coupons_enabled;
}

Edit: okay you need to check if page is checkout or cart then run the script. I have added condition in code.


推荐阅读