It’s not uncommon to be asked to implement minimum checkout requirements to WooCommerce Cart. The common requirements are:
- Minimum Dollar Amount Per Order
- Minimum Number of Products Per Order or Per Products
Luckily, there is a great tutorial for implementing these by Yojance Rabelo.
But the other day, I got a request to implement minimum dollar amount PER PRODUCT…not too common, but not never-heard-of either.
So I decided to tweak Yojance’s Minimum Dollar Per Order code to set minimum order amount for a product instead of for the entire order:
Here is the code. You want to add this in functions.php
/* * Minimum Order Dollar Amount for a specific product * In this example, the product's subtotal must be $100 or more */ add_action( 'woocommerce_check_cart_items', 'product_set_min_total' ); function product_set_min_total() { // Only run in the Cart or Checkout pages if( is_cart() || is_checkout() ) { global $woocommerce; // Set minimum product total $minimum_cart_total = 100; // Total we are going to be using for the Math // This is before taxes and shipping charges $total = 0; //foreach products in the cart () foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); //this example's product id = 3288 $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key ); if (strcmp($cart_item['product_id'], "3288") == 0 ) { $subtotal = $_product->get_price() * (float)$cart_item['quantity']; $total = $total + $subtotal; } } // Will display a message along the lines of // A Minimum of 100 USD is required before checking out. (Cont. below) if( $total <= $minimum_cart_total ) { // Display our error message wc_add_notice( sprintf( '<strong>A Minimum subtotal of $%s %s is required for Sample Product before checking out.</strong>'.'<br />Current cart\'s total: $%s %s', $minimum_cart_total, get_option( 'woocommerce_currency'), $total, get_option( 'woocommerce_currency') ), 'error' ); } }