简体   繁体   中英

Remove conditionally "proceed to checkout" button from minicart in Woocommerce

In woocommerce I am currently seeking to add a function in my theme's functions.php file, if two conditions are met. Then, using elseif() , deploy the function if only one condition is met.

The code is as follows:

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' 
            );
        }
    }
}

What I am trying to do is hide the checkout button for the woocommerce widget if the minimum order amount is not met. However, different pages have different minimums.

I'm trying to hide the checkout button if the cart isn't equal to $150. But for one page in particular, I only want the cart to have a minimum of $100.

Note that the hook that you are using is only made for minicart widget, so you don't need to test that in an IF statement.

You are making that much more complicated that it should be. Try the following revisited code instead:

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' 
        );
    } 
}

Code goes in function.php file of your active child theme (or active theme). It should better work now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM