lundi 8 octobre 2018

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.

Aucun commentaire:

Enregistrer un commentaire