Reputation: 447
I'm trying to Set a minimum order amount in dollars of 200$ EXCEPT for a bunch of products (about 5 of them) that can be added individually regardless of the amount in cart.
So far i found this code, which seems to be fine for blocking the checkout if minimum is not reached:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
global $woocommerce;
$minimum = 200;
if ( $woocommerce->cart->get_cart_total() < $minimum ) {
$woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) );
}
}
Im trying now to figure out how to exclude certain product_id's or even certain category_id's
There is similar question here, but not exactly as mine, and im not sure on how to modify the code to addapt to my needs
Woocomerce set category minimum cart
Thanks Kind regards
Now after some changes I have a few questions/problems with the code as explained in my answer bellow. The code I´m using is the following:
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, $product;
$i=0;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) :
// Set minimum cart total
$minimum_cart_total = 200;
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->subtotal;
// See if any product is from the STOCK category or not
if ( has_term( '481', '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'] );
endif;
endforeach;
foreach ( $woocommerce->cart->cart_contents as $product ) :
if ( has_term( '481', 'product_cat', $product['product_id'] ) ) :
// 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( $subtotal_cat <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required from stock category before checking out.</strong>'
.'<br />Current cart\'s total: %s %s excl. TAX',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
endif;
endforeach;
}
}
Ok, I managed to make it work all of it except for the categories thing. Let me explain my self.
Right now it is checking if the products inthe cart belongs to one category and if it does, then check if it is more than 200 $. If it is not above 200$ the total amount of the cart and there is a product from the specified category in the cart, then it shows the error message. I need to add more than one category to this function, but I really dont know how to do it. I tried the code bellow, and even if it does not give any error, it does not recognize the categories.If i use just one category ID it works fine:
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, $product;
$i=0;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) :
// Set minimum cart total
$minimum_cart_total = 200;
// 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,482', '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'] );
endif;
endforeach;
if ( has_term( '481,482', 'product_cat', $product['product_id'] ) ) :
// 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
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required from stock category before checking out.</strong>'
.'<br />Current cart\'s total: %s %s excl. TAX',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
endif;
}
}
Thanks
Upvotes: 0
Views: 2323
Reputation: 447
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' );
}
}
}
Upvotes: 2