• Resolved joshlo

    (@joshlo)


    Hey guys

    Basiclaly I’m writing a function that whenever someone adds items to their cart that add up to over £250 the order doesn’t let them checkout. So far with this code their purchase is blocked but they are still allowed in the checkout page. I’d like to remove or hide the proceed to checkout button or display the message when proceed to checkout is pushed. Here is what I have so far. Any suggestions? Many thanks in advance.

    /**
     * Set maximum order amount *
     */
    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
     
    function wc_minimum_order_amount() {
        // Set this variable to specify a maximum order value
        $maximum = 250;
    	// get current user from wordpress
    	$user = wp_get_current_user();
    	// if users role is any of these in the array
    $allowed_roles = array('editor', 'author', 'wholesale_customer');
    if( array_intersect($allowed_roles, $user->roles ) ) { 
       // Stuff here for allowed roles
    
        if ( WC()->cart->total > $maximum ) {
    
            if( is_cart() ) {
    remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
                wc_add_notice(
                    sprintf( 'Your current order total is %s — this exceeds the %s limit. ' , 
                        wc_price( WC()->cart->total ), 
                        wc_price( $maximum )
                    ), 'error' 
                );
    			return false;
    			
    
            } else {
    			remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
                wc_add_notice( 
                    sprintf( 'Your current order total is %s — this exceeds the order maximum of %s to place your order.' , 
                        wc_price( WC()->cart->total ), 
                        wc_price( $maximum )
                    ), 'error' 
    				
                ); 
    			return false;
    			
    
            }
        }
    }
    }
    • This topic was modified 5 years, 11 months ago by joshlo.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @joshlo – There are a few changes I would make here.. First, I would suggest using the woocommerce_proceed_to_checkout to attach to your function. Additionally, I would not suggest using a notice, as those don’t always load when they should on the cart/checkout page.

    Here’s what I would suggest:

    function disable_ptc_max250() { 
        $maximum = 250;
        
        if( $maximum < WC()->cart->total ){
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
            echo '<p class="checkout-button button alt">Cart total must be lower than $' . $maximum . '</p>'; 
        }  
    }
    
    add_action( 'woocommerce_proceed_to_checkout', 'disable_ptc_max250', 1 );

    This will use the same button styling on the cart page as the ‘Proceed to Checkout’ button, but will instead display: Cart total must be lower than $250
    Since I included a variable in the string, the $maximum amount will be pulled in without needing to be hardcoded within that `<p> element.

    Here’s what it looks like for me when <strong>under</strong> $250: https://d.pr/i/1Ijabg
    Here’s what it looks like for me when <strong>over</strong> $250: https://d.pr/i/ZaADO1

    The echo of the <p> element ensures that this is called on page load, so the button will consistently change as the cart total is updated.

    Let me know if you have any questions! 😁

    – Joey

    Just wanted to add here as well, I put together several other scenarios as well: https://gist.github.com/jrick1229/0e64409a5e54f2f63dc70365b646d270
    I’m putting this here in case anyone else comes across this in their search.

    – Joey

    Plugin Support Tseten a11n

    (@tibetanitech)

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Trying to block checkout page’ is closed to new replies.