skip to Main Content

I want to restrict the customer to place order of less than INR 400 for 2 states. So far i have tried this

add_action( 'woocommerce_check_cart_items', 'set_min_total' );

function set_min_total() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set minimum cart total
        $minimum_cart_total = 400;

        ?>
            <script type="text/javascript">
                jQuery(document).ready(function(){
                    jQuery('#billing_state').on('change',function(){
                        var optionText = jQuery("#billing_state option:selected").val();
                    });
                });
                <?php $selected_state = '<script type="text/javascript">optionText</script>'?>
            </script>;


        <?php

        $allowed_state = 'UP';
        if($allowed_state != $selected_state) {

            $total = WC()->cart->subtotal;

            if( $total <= $minimum_cart_total  ) {
                // Display our error message
                wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
                    .'<br />Current cart's total: %s %s',
                    $minimum_cart_total,
                    get_option( 'woocommerce_currency'),
                    $total,
                    get_option( 'woocommerce_currency') ),
                'error' );
            }
        }
    }
}

but it didn’t worked, please advice where am i doing wrong.

2

Answers


  1. add_action( 'woocommerce_check_cart_items', 'cldws_set_min_total');
        function cldws_set_min_total() {
          // Only run in the Cart or Checkout pages
          if( is_cart() || is_checkout() ) {
            global $woocommerce;
            // Set minimum cart total
            $minimum_cart_total = 100;
                // A Minimum of 100 AUD is required before checking out.
            $total = WC()->cart->subtotal;
                // Compare values and add an error is Cart's total
              if( $total <= $minimum_cart_total  ) {
                 // Display our error message
              wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
                .'<br />Current cart's total: %s %s',
                $minimum_cart_total,
                get_option( 'woocommerce_currency'),
                $total,
                get_option( 'woocommerce_currency') ),
              'error' );
            }
          }
        }
    

    Please check with above code , here amount is 100 you can use as you need

    Login or Signup to reply.
  2. Add the follows codes snippet to achieve the above task –

    /**
     * Set a minimum order amount for checkout
     */
    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
    
    function wc_minimum_order_amount() {
        // Set minimum order value
        $minimum = 400;
    
        if ( WC()->cart->total < $minimum ) {
    
            if( is_cart() ) {
    
                wc_print_notice( 
                    sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' , 
                        wc_price( WC()->cart->total ), 
                        wc_price( $minimum )
                    ), 'error' 
                );
    
            } else {
                $billing_state = ( $_POST && isset( $_POST['billing_state'] ) ) ? $_POST['billing_state'] : '';
                if( in_array( $billing_state, array( 'UP' ) ) ) {
                    wc_add_notice( 
                        sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' , 
                            wc_price( WC()->cart->total ), 
                            wc_price( $minimum )
                        ), 'error' 
                    );
                }
            }
        }
    }
    

    Codes goes to your active theme’s functions.php.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search