php - 从 Woocommerce 中的迷你购物车有条件地删除“继续结帐”按钮
问题描述
functions.php
在 woocommerce 中,如果满足两个条件,我目前正在寻求在我的主题文件中添加一个函数。然后,elseif()
如果仅满足一个条件,则使用 部署该功能。
代码如下:
add_action( 'woocommerce_widget_shopping_cart_before_buttons' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
$minimum = 150;
$minimum2 = 100;
if ( is_page([232]) && WC()->cart->subtotal < $minimum2 ) {
if( 'woocommerce_widget_shopping_cart' ) {
wc_print_notice(
sprintf( 'Your current order total does not meet the %s minimum' ,
wc_price( $minimum2 )
), 'error'
);
remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );
}
else {
wc_add_notice(
sprintf( 'Your current order total does not meet the %s minimum' ,
wc_price( $minimum2 )
), 'error'
);
}
}
elseif ( WC()->cart->subtotal < $minimum ) {
if( 'woocommerce_widget_shopping_cart' ) {
wc_print_notice(
sprintf( 'Your current order total does not meet the %s minimum',
wc_price( $minimum )
), 'error'
);
remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );
}
else {
wc_add_notice(
sprintf( 'Your current order total does not meet the %s minimum' ,
wc_price( $minimum )
), 'error'
);
}
}
}
如果未达到最低订单金额,我想要做的是隐藏 woocommerce 小部件的结帐按钮。但是,不同的页面有不同的最小值。
如果购物车不等于 150 美元,我会尝试隐藏结帐按钮。但特别是对于一页,我只希望购物车至少有 100 美元。
解决方案
请注意,您使用的钩子仅适用于 minicart 小部件,因此您无需在IF
语句中对其进行测试。
您正在使应该变得更加复杂。请尝试以下重新访问的代码:
add_action( 'woocommerce_widget_shopping_cart_before_buttons' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
$min_amount = is_page([232]) ? 100 : 150;
if( WC()->cart->subtotal < $min_amount ) {
remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );
wc_add_notice(
sprintf( 'Your current order total does not meet the %s minimum' ,
wc_price( $min_amount )
), 'error'
);
}
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。它现在应该更好地工作。
推荐阅读
- python - 我想从同一个文件python的不同工作表中获取一些数据
- javascript - 没有服务器的网络工作者
- .net - 控制器中的JQuery ajax发布参数为空
- c# - 过滤自动完成列表中的数据
- javascript - 如何从组件初始化Angular服务,自定义类?
- python - 如何对里面有列表的字典进行排序?
- mysql - 在 docker-compose 中使用卷时,Mariadb 表被删除
- reactjs - 自定义 React Navigation Dark 和 Light 主题未应用于子组件
- swift - SwiftUI 从 String(Base64) 解码图像
- codeblocks - 如何在代码块中配置 msys2?