WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
2.1.3 Minimum Order Required (6 posts)

  1. dcbaldwin1
    Member
    Posted 4 months ago #

    WordPress 3.8.1 with WooCommerce 2.1.3 on Media Temple gs...

    Does anyone know of a tested method to require a minimum order amount for 2.1.3? I am currently using the code snippet below. It does prevent you from accessing the checkout page if your order does not meet the minimum, however, if you do meet the minimum and successfully checkout, you are still redirected back to the cart, which is now empty because the order went through. Before the redirect back to the cart, the site does attempt to load the order-recieved page (endpoint), but the redirect happens before the page loads that has the order confirmation, making the purchaser think their order did not go through when it did.

    add_action('the_post','minimum_order_func',10);
    function minimum_order_func(){
    global $woocommerce;
    if ( is_checkout() ) {
    	$minorder = 150;
    	if( $woocommerce->cart->subtotal < $minorder){
    		 $woocommerce->add_error( sprintf(__('Sorry, you have not met the minimum order amount of $' . $minorder . '. Once you do meet this minimum, you can proceed with the checkout. <a href="%s/store">Continue Shopping &rarr;</a>', 'woocommerce'), home_url()) );
    		    wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) );
    		   			exit;
    		}
    	}
    }

    https://wordpress.org/plugins/woocommerce/

  2. dcbaldwin1
    Member
    Posted 4 months ago #

    Okay I tried this (hacky) work-around which does prevent the redirect after successful checkout, but, the error message still shows on the order-received page.

    add_action('the_post','minimum_order_func',10);
    function minimum_order_func(){
    global $woocommerce;
    if ( is_checkout() ) {
    
    	$minorder = 150;
    
    	$url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    
    	if( $woocommerce->cart->subtotal < $minorder ){
    
    		 $woocommerce->add_error( sprintf(__('Sorry, you have not met the minimum order amount of $' . $minorder . '. Once you do meet this minimum, you can proceed with the checkout. <a href="%s/store">Continue Shopping &rarr;</a>', 'woocommerce'), home_url()) );
    
    		 	if (false !== strpos($url,'order-received')) { //order recieved enpoint exists
    			    //do nothing
    			} else {
    		    wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) );
    		   			exit;
    		   	}
    
    		}
    	}
    }
  3. Roy Ho
    Member
    Posted 4 months ago #

  4. dcbaldwin1
    Member
    Posted 4 months ago #

    Thanks Roy. The snippet provided at http://docs.woothemes.com/document/minimum-order-amount/ doesn't seem to do anything at all.

  5. dcbaldwin1
    Member
    Posted 4 months ago #

    Okay I have accomplished what I want, but I still feel it's 'hacky.' Below is my code. I moved the output of the error into the else block:

    add_action('the_post','minimum_order_func',10);
    function minimum_order_func(){
    	global $woocommerce;
    
    	if ( is_checkout() ) {
    
    		$minorder = 150;
    
    		$url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    
    		if( $woocommerce->cart->subtotal < $minorder ){
    
    		 	if (false !== strpos($url,'order-received')) { //order recieved enpoint exists
    			    //do nothing
    			} else {
    
    			 $woocommerce->add_error( sprintf(__('Sorry, you have not met the minimum order amount of $' . $minorder . '. Once you do meet this minimum, you can proceed with the checkout. <a href="%s/store">Continue Shopping →</a>', 'woocommerce'), home_url()) );
    
    		     wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) );
    
    		   	 exit;
    		   	}
    
    		}
    	}
    }
  6. vark
    Member
    Posted 4 months ago #

Reply

You must log in to post.

About this Plugin

About this Topic