Viewing 15 replies - 1 through 15 (of 44 total)
  • andrianiannalisa

    (@andrianiannalisayahooit)

    So do I.
    I’m looking for a very simple add-on for woocommerce in WordPress to allow me to set a minimum order value. If the order total is below the set value the user is told the value is too low and directed to add more to their card.

    Does anyone know if this is possible?

    bheadrick

    (@bheadrick)

    there’s a redirect in place that if someone tries to access the checkout and there’s no items in the cart, it redirects them back to cart. the action that’s called just before it is woocommerce_before_checkout_process

    so,

    add_action('woocommerce_before_checkout_process','minimum_order_func');
    
    function minimum_order_func(){
    global $woocommerce;
    $minorder = 20;
    if( $woocommerce->cart->get_cart()->cart_contents_total<$minorder){
    $woocommerce->add_error( sprintf(__('Sorry, you have not met the minimum order amount of $' . $minorder . '. <a href="%s">Return to homepage &rarr;</a>', 'woocommerce'), home_url()) );
    }
    }

    andrianiannalisa

    (@andrianiannalisayahooit)

    Where do I add this code?

    I’m not an expert in PHP ??

    Thank’s very much!!

    andrianiannalisa

    (@andrianiannalisayahooit)

    Do I insert it before of “woocommerce_before_checkout_process” (with parameters I want the minimum order) in the class-wc-checkout.php?

    andrianiannalisa

    (@andrianiannalisayahooit)

    ok it works fine!!

    you are great!
    Another question … if I would like to redirect the user to another page, and not at the home, how can I do?

    Thank’s!

    andrianiannalisa

    (@andrianiannalisayahooit)

    mmm .. I did a test with a minimum order but does not work …
    Perhaps because the minimum amount in my cart is in € and not in dollars?

    bheadrick

    (@bheadrick)

    you could wrap it in a basic plugin, or you could copy it into your theme’s functions.php

    I just tested this, and it doesn’t work. Seems you need a different action hook. haven’t found the right one yet.

    bheadrick

    (@bheadrick)

    definitely don’t modify the woocommerce files (except for the template files copied to your theme)

    May Didly

    (@may-didly)

    The code above didn’t work for me. I played around with a bit and got it to work. I don’t know php so it might not be the best way of coding it.

    add_action('woocommerce_before_checkout_process','minimum_order_func');
    
    function minimum_order_func(){
    	global $woocommerce;
    	$minorder = 50;
    
    	if( $woocommerce->cart->subtotal<$minorder ){
    		$woocommerce->add_error( sprintf(__('Sorry, you have not met the minimum order amount of $' . $minorder . '. <a href="%s">Return to homepage &rarr;</a>', 	'woocommerce'), home_url()) );
    	}
    }
    andrianiannalisa

    (@andrianiannalisayahooit)

    I have pasted into the file class-wc-checkout.php before “woocommerce_before_checkout_process”, but the warning “Sorry, you have not met the minimum order amount…” appears also if the minimum order was made.

    bheadrick

    (@bheadrick)

    OK, I got this working. apparently the *checkout_process actions actually fire after you click “place order” not when you first hit the checkout page. We need the function to fire as soon as the page loads.

    add_action('the_post','minimum_order_func',10);
    function minimum_order_func(){
    global $woocommerce;
    if (is_checkout()){
    $minorder = 50;
    if( $woocommerce->cart->subtotal < $minorder){
     $woocommerce->add_error( sprintf(__('Sorry, you have not met the minimum order amount of $' . $minorder . '. <a href="%s">Return to homepage &rarr;</a>', 'woocommerce'), home_url()) );
        wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) );
       			exit;
    }}}

    Don’t hack up the woocommerce plugin, cos when they release an update, your changes will be gone. The point of hooks is so you can apply your mods outside of the core code.

    Most people put this kind of stuff at the bottom of their theme’s functions.php, although it’s sometimes a good idea to wrap it up in a separate plugin. You’ll be fine to put this in your functions.php, though.

    andrianiannalisa

    (@andrianiannalisayahooit)

    Perfect!
    Now it seems to work fine!

    You are great!!

    Thanks for this.

    How to add a check to see if the customer chose delivery before showing the message? I have a store that customers can order food for pickup or delivery. Pickup has no minimum order value, but delivery has a $15 minimum.

    i had this working a few weeks back on a demo site im working on.

    getting back to working on the site, having updated wp and wc this does not seem to work any longer…

    could anyone verify this?

    Hi guys – Thank you collective brainpower for solving this one!

    I’m trying to add this rule to my site…
    but can someone please clarify where precisely I should include this?

    on the theme’s functions.php?
    on the woocommerce plugins functions.php?

    somewhere else entirely?

    and is this exactly how i should paste it in?

    add_action(‘the_post’,’minimum_order_func’,10);
    function minimum_order_func(){
    global $woocommerce;
    if (is_checkout()){
    $minorder = 50;
    if( $woocommerce->cart->subtotal < $minorder){
    $woocommerce->add_error( sprintf(__(‘Sorry, you have not met the minimum order amount of $’ . $minorder . ‘. Return to homepage →‘, ‘woocommerce’), home_url()) );
    wp_redirect( get_permalink( woocommerce_get_page_id( ‘cart’ ) ) );
    exit;
    }}}

Viewing 15 replies - 1 through 15 (of 44 total)
  • The topic ‘Set a minimum order for cart total?’ is closed to new replies.