wordpress - 添加优惠券后隐藏和刷新 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;
}
解决方案
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.
推荐阅读
- c# - 当我使用 iText 5 .NET 将 HTML 字符串转换为 PDF 时,内联样式表不起作用
- svg - 在 SVG 中,我们可以在 desc 标签内格式化文本吗?
你好
世界 - php - 检查两个集合的差异
- android - 启用远程js调试时React Native应用程序没有网络连接
- angular - Mat datepicker 仅格式化多个选择器之一
- php - 如何将输入限制为两倍?
- vb.net - 我需要帮助将此 C# Linq 查询转换为 VB.net。我不太了解某些方面
- flutter - 为什么 SearchDelegate 的 buildSuggestion 方法不起作用?
- java - 使用哈希映射(键/值对)处理帖子表单的成语
- artifactory - Artifactory OSS版无维护操作