[英]Remove "proceed to checkout" button based on WooCommerce cart total
I am trying to disable and enable checkout according to the cart total.我正在尝试根据购物车总数禁用和启用结帐。
Disable proceed checkout button works but enable it does not give the desired result, see the code below:禁用继续结帐按钮有效,但启用它不会给出所需的结果,请参见下面的代码:
function disable_checkout() {
$maximum = 500;
if( WC()->cart->total < $maximum ){
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
echo '<p class="checkout-button button alt">Wholesale Orders is only valid on order more than ' . $maximum . ' Euros.</p>';
} else {
echo '<p class="checkout-button button alt">Proceed to checkout</p>';
add_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout', 1 );
Can someone guide me through this process?有人可以指导我完成这个过程吗?
Some comments/suggestions regarding your code attempt:关于您的代码尝试的一些评论/建议:
woocommerce_check_cart_items
action hook instead您可以改用woocommerce_check_cart_items
操作挂钩WC()->cart->get_cart_contents_total()
to get cart total.使用WC()->cart->get_cart_contents_total()
获取购物车总数。 This is the total of items in the cart, but after discounts.这是购物车中的商品总数,但已打折。So you get:所以你得到:
function action_woocommerce_check_cart_items() {
// Setting
$minimum = 500;
// Get cart total
$cart_total = WC()->cart->get_cart_contents_total();
// Less than the minimum
if ( $cart_total < $minimum ) {
// Notice
wc_add_notice( sprintf( __( 'Wholesale Orders is only valid on order more than %s Euros.', 'woocommerce' ), $minimum ), 'error' );
// Optional: remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.