Minimum Cart Amount for Specific Product Categories in Woocommerce

Minimum cart amount for specific product categories in WooCommerce

The right hook to be used in this case is woocommerce_check_cart_items this way:

add_action( 'woocommerce_check_cart_items', 'check_cart_outlet_items' );
function check_cart_outlet_items() {
$categories = array('OUTLET'); // Defined targeted product categories
$threshold = 30; // Defined threshold amount

$cart = WC()->cart;
$cart_items = $cart->get_cart();
$subtotal = $cart->subtotal;
$subtotal -= $cart->get_cart_discount_total() + $cart->get_cart_discount_tax_total();
$found = false;

foreach( $cart_items as $cart_item_key => $cart_item ) {
// Check for specific product categories
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$found = true; // A category is found
break; // Stop the loop
}
}

if ( $found && $subtotal < $threshold ) {
// Display an error notice (and avoid checkout)
wc_add_notice( sprintf( __( "You must order at least %s" ), wc_price($threshold) ), 'error' );
}
}

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

Minimum cart item quantity for specific product categories in WooCommerce

Since WooCommerce 3, your actual code is outdated and not convenient… There is multiple ways:

1). The best way: Set up the minimum quantity at product level (for a product category):

// On single product pages
add_filter( 'woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2 );
function min_qty_filter_callback( $args, $product ) {
$categories = array('Noten'); // The targeted product category(ies)
$min_qty = 5; // The minimum product quantity

$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

if( has_term( $categories, 'product_cat', $product_id ) ){
$args['min_value'] = $min_qty;
}
return $args;
}

// On shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_args', 'min_qty_loop_add_to_cart_args', 10, 2 );
function min_qty_loop_add_to_cart_args( $args, $product ) {
$categories = array('Noten'); // The targeted product category
$min_qty = 5; // The minimum product quantity

$product_id = $product->get_id();

if( has_term( $categories, 'product_cat', $product_id ) ){
$args['quantity'] = $min_qty;
}
return $args;
}

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

2). Alternative way: Checking cart items and displaying an error message (similar to your code):

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
$categories = array('Noten'); // The targeted product category
$min_item_qty = 5; // Minimum Qty required (for each item)
$display_error = false; // Initializing

// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
$item_quantity = $cart_item['quantity']; // Cart item quantity
$product_id = $cart_item['product_id']; // The product ID

// For cart items remaining to "Noten" producct category
if( has_term( $categories, 'product_cat', $product_id ) && $item_quantity < $min_item_qty ) {
wc_clear_notices(); // Clear all other notices

// Add an error notice (and avoid checkout).
wc_add_notice( sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.', $min_item_qty , $item_quantity ), 'error' );
break; // Stop the loop
}
}
}

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


To make it work for parent product category too, you will also add this custom function:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;

if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}

// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}

// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

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

Then in the existing code, you will replace:

has_term( $category, 'product_cat', $product_id )

by

has_product_categories( $category, $product_id )

That will allow you to handle parent product categories too.

Set minimum order quantity by category (Woocommerce)

Finaly found the solution. For those interested, here is the custom function :

add_action('woocommerce_check_cart_items', 'custom_set_min_total');
function custom_set_min_total()
{
if (is_cart() || is_checkout()) {

global $woocommerce, $product;
$i = 0;

foreach ($woocommerce->cart->cart_contents as $product) :
$minimum_cart_product_total = 3;

if (has_term('nettoyage', 'product_cat', $product['product_id'])) :
$total_quantity += $product['quantity'];
endif;

endforeach;

foreach ($woocommerce->cart->cart_contents as $product) :
if (has_term('nettoyage', 'product_cat', $product['product_id'])) :
if ($total_quantity < $minimum_cart_product_total && $i == 0) {
wc_add_notice(
sprintf(
'<strong>A Minimum of %s products is required from the nettoyage category before checking out.</strong>'
. '<br />Current number of items in the cart: %s.',
$minimum_cart_product_total,
$total_quantity
),
'error'
);
}
$i++;
endif;
endforeach;
}
}

Set minimum Order amount for specific Products or Categories in WooCommerce

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).

Set a minimum quantity for cart items from specific WooCommerce product category

You need first to count the number of items from the targeted product category… then you can display the error notice when the number of items are below the minimum defined:

add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
function wc_min_item_required_qty() {
$category = 'Games'; // The targeted product category
$min_qty = 4; // Minimum Qty required (for each item)
$qty_count = 0; // Initializing

// Loop through cart items
foreach(WC()->cart->get_cart() as $item ) {
// Count items from the targeted product category
if( has_term( $category, 'product_cat', $item['product_id'] ) ) {
$qty_count += $item['quantity'];
}
}

// Display error notice avoiding checkout
if( $qty_count != 0 && $qty_count < $min_qty ) {
wc_clear_notices(); // Clear all other notices

// Add an error notice (and avoid checkout).
wc_add_notice( sprintf(
__("You should pick at least %s items from %s category.", "woocommerce"),
'<strong>' . $min_qty . '</strong>',
'<strong>' . $category . '</strong>'
), 'error' );
}
}

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

Sample Image

Set a minimum amount in Woocommerce cart except for one category

I had to do a lot of changes, but now it is working PERFECT. Here is the code I used:

add_action( 'woocommerce_check_cart_items', 'cart_set_min_total' );
function set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {

global $woocommerce, $product;
$i=0;
// Minimum order checking
$minimumCheck = false;
// Set minimum cart total
$minimum_cart_total = 200;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) {
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->total;

// See if any product is from the STOCK category or not
if ( has_term( '481', 'product_cat', $product['product_id'] ) || has_term( '482', 'product_cat', $product['product_id'] ) ) {
$minimumCheck = true;
//Get price of that product
$regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale
//echo $regular_price."<br>";
$total = $regular_price * $product['quantity'];
//echo $total."<br>";
$subtotal_cat += $total; //get total of
//echo $subtotal_cat;
//$category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );

}
if (has_term( '503', 'product_cat', $product['product_id']) || has_term( '495', 'product_cat', $product['product_id'] ) ) {
//Get price of that product
$regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale
//echo $regular_price."<br>";
$total = $regular_price * $product['quantity'];
//echo $total."<br>";
$subtotal_cat += $total; //get total of
//echo $subtotal_cat;
//$category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );
}

}

if ( $minimumCheck && $subtotal_cat <= $minimum_cart_total) {

// 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 200 USD is required before checking out. (Cont. below)
// Current cart total: 6 USD
wc_add_notice( sprintf( '<strong>A Minimum of %s %s excl. TAX is required category before checking out.</strong>'
.'<br />Current cart\'s total: %s %s excl. TAX',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$subtotal_cat,
get_option( 'woocommerce_currency') ),
'error' );
}

}

}


Related Topics



Leave a reply



Submit