简体   繁体   English

根据 WooCommerce 购物车总数删除“继续结帐”按钮

[英]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:关于您的代码尝试的一些评论/建议:

  • You can use the woocommerce_check_cart_items action hook instead您可以改用woocommerce_check_cart_items操作挂钩
  • Use 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.

相关问题 Woocommerce - 禁用购物车页面中的“继续结账”按钮 - Woocommerce - disable «proceed to checkout» button in cart page 在 WooCommerce 中根据用户角色和购物车总数更改结帐时的订单按钮文本 - Change order button text on checkout based on user role and cart total in WooCommerce 有条件地从 Woocommerce 的迷你车中删除“继续结账”按钮 - Remove conditionally "proceed to checkout" button from minicart in Woocommerce 有条件地删除 Braintree 中 WooCommerce 插件的“继续结帐”按钮 - Conditional remove "proceed to checkout" button in Braintree for WooCommerce plugin 如何将继续进行到结帐-购物车woocommerce - How to move Proceed to checkout - cart woocommerce 如何从购物车和结帐页面woocommerce中删除订单总额 - How to remove order total from cart and checkout page woocommerce Woocommerce条件结帐字段和基于国家和购物车总额的欧盟增值税 - Woocommerce conditional checkout fields and Eu VAT based on country and cart total 根据 WooCommerce 购物车总数添加或删除特定购物车项目 - Add or remove specific cart Item based on WooCommerce cart total 避免继续结帐按钮文本刷新到 WooCommerce 购物车页面中的默认文本 - Avoid proceed-to-checkout-button text refreshing to default text in WooCommerce cart page 更改“继续付款”按钮位置Woocommerce - Change “Proceed To Checkout” Button Location Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM