Skip to content
Advertisement

Set minimum Order amount for specific Products or Categories in WooCommerce

I’ve searched extensively to see if other folks had this situation and received an answer with no luck.

Essentially, I have two items customers can add to their cart. I want to make it so they cannot checkout with either of those items if their subtotal is not $15 or more.

Having the ability to just drop their IDs into the code would be fine. Or, I can assign them to the same category and set this minimum by category.

So far all I have is the basic PHP that sets a universal minimum. I just need some help figuring out how to limit this to meet my needs.

I’m a designer not a dev, so any help is much appreciated.

    // Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_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 = 10;

        // Total we are going to be using for the Math
        // This is before taxes and shipping charges
        $total = WC()->cart->subtotal;

        // Compare values and add an error is Cart's total
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of 10 USD is required before checking out. (Cont. below)
        // Current cart total: 6 USD 
        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' );
        }
    }
}

Advertisement

Answer

2020 Update

Setting minimum value for some products categories or products ID’s in cart for Cart and Checkout pages only.

This untested snippet made of this and this from wooThemes, and this too:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
 
function wc_minimum_order_amount() {
    ##  SETTINGS  ##
    $minimum_amount = 50; // Define a minimum order amount
    $category_ids   = array( 17, 18 ); // Define your category ids in the array (or an empty array to disable)
    $product_ids    = array( 64, 67, 78 ); // Define your product ids in the array (or an empty array to disable)

    // Only on cart or checkout pages
    if( WC()->cart->is_empty() || ! ( is_cart() || is_checkout() ) ) 
        return; // Exit

    $total_amount = WC()->cart->subtotal; // Items subtotal including taxes

    if ( $total_amount < $minimum_amount ) {
        $needs_minimum_amount = false; // Initializing

        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product_id   = $cart_item['product_id'];
            $variation_id = $cart_item['variation_id'];
            
            // 1. Check for matching product categories
            if( sizeof($category_ids) > 0 ) {
                $taxonomy = 'product_cat';

                if ( has_term( $category_ids, $taxonomy, $product_id ) ) { 
                    $needs_minimum_amount = true;
                    break; // Stop the loop
                }
            }

            // 2. Check for matching product Ids
            if( sizeof($product_ids) > 0 ) {
                if ( array_intersect( $product_ids, array($product_id, $variation_id) ) ) { 
                    $needs_minimum_amount = true;
                    break; // Stop the loop
                }
            }
        }

        if( $needs_minimum_amount ) {
            wc_print_notice( sprintf( 
                __("You must have an order with a minimum of %s to place your order. Your current order total is %s."), 
                wc_price( $minimum_amount ), 
                wc_price( $total_amount )
            ), 'error' );
        }
    }
}

Code goes in functions.php file of your active child theme (active theme). Tested and works.

You can set your categories ID’s, your products ID’s and the minimum order value amount.


— — … I m p o r t a n t … N o t e … — —

This hooks are working for messages. But to avoid users from clicking you need to add Javascript/jQuery and maybe with ajax too (client side detection).

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement