-
-
Save bekarice/2061fcbdc3ad2b80ddf8 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* only copy the opening php tag if needed | |
* tutorial at: http://swwp.co/6l | |
*/ | |
/** | |
* Renders notices and prevents checkout if the cart | |
* does not contain a minimum number of products in a category | |
*/ | |
function sww_check_category_for_minimum() { | |
// set the minimum quantity in the category to purchase | |
$min_quantity = 6; | |
// set the id of the category for which we're requiring a minimum quantity | |
$category_id = 15; | |
// get the product category | |
$product_cat = get_term( $category_id, 'product_cat' ); | |
$category_name = '<a href="' . get_term_link( $category_id, 'product_cat' ) . '">' . $product_cat->name . '</a>'; | |
// get the quantity category in the cart | |
$category_quantity = sww_get_category_quantity_in_cart( $category_id ); | |
if ( $category_quantity < $min_quantity ) { | |
// render a notice to explain the minimum | |
wc_add_notice( sprintf( 'Sorry, you must purchase at least %1$s products from the %2$s category to check out.', $min_quantity, $category_name ), 'error' ); | |
} | |
} | |
add_action( 'woocommerce_check_cart_items', 'sww_check_category_for_minimum' ); | |
/** | |
* Returns the quantity of products from a given category in the WC cart | |
* | |
* @param int $category_id the ID of the product category | |
* @return in $category_quantity the quantity of products in the cart belonging to this category | |
*/ | |
function sww_get_category_quantity_in_cart( $category_id ) { | |
// get the quantities of cart items to check against | |
$quantities = WC()->cart->get_cart_item_quantities(); | |
// start a counter for the quantity of items from this category | |
$category_quantity = 0; | |
// loop through cart items to check the product categories | |
foreach ( $quantities as $product_id => $quantity ) { | |
$product_categories = get_the_terms( $product_id, 'product_cat' ); | |
// check the categories for our desired one | |
foreach ( $product_categories as $category ) { | |
// if we find it, add the line item quantity to the category total | |
if ( $category_id === $category->term_id ) { | |
$category_quantity += $quantity; | |
} | |
} | |
} | |
return $category_quantity; | |
} |
Hi Bekarice,
I'm getting an eval()'d error in line 45. I've added this snippet code in my "Code Snippet" plugin.
Would you please tell me what's causing that? Thank you.
function sww_check_category_for_minimum() {
// set the minimum quantity in the category to purchase
$min_quantity = 2;
// set the id of the category for which we're requiring a minimum quantity
$category_id = 7;
// get the product category
$product_cat = get_term( $category_id, 'product_cat' );
$category_name = '<a href="' . get_term_link( $category_id, 'product_cat' ) . '">' . $product_cat->name . '</a>';
// get the quantity category in the cart
$category_quantity = sww_get_category_quantity_in_cart( $category_id );
if ( $category_quantity < $min_quantity ) {
// render a notice to explain the minimum
wc_add_notice( sprintf( 'Sorry, you must purchase at least %1$s products from the %2$s category to check out.', $min_quantity, $category_name ), 'error' );
}
}
add_action( 'woocommerce_check_cart_items', 'sww_check_category_for_minimum' );
/**
-
Returns the quantity of products from a given category in the WC cart
-
@param int $category_id the ID of the product category
-
@return in $category_quantity the quantity of products in the cart belonging to this category
*/
function sww_get_category_quantity_in_cart( $category_id ) {// get the quantities of cart items to check against
$quantities = WC()->cart->get_cart_item_quantities();// start a counter for the quantity of items from this category
$category_quantity = 0;// loop through cart items to check the product categories
foreach ( $quantities as $product_id => $quantity ) {$product_categories = get_the_terms( $product_id, 'product_cat' ); // check the categories for our desired one foreach ( $product_categories as $category ) { // if we find it, add the line item quantity to the category total if ( $category_id === $category->term_id ) { $category_quantity += $quantity; } }
}
return $category_quantity;
}
this does not work for variations you need to put in checks if product is a Variable Product lease ammend with below
$product_categories = get_the_terms( $product_id, 'product_cat');
//check if it works else its a variation
if(!$product_categories){
$variation = wc_get_product($product_id);
//get variation parent id number
$parent_product_id = $variation->get_parent_id();
//get categories of parent id number
$product_categories = get_the_terms( $parent_product_id, 'product_cat');
}
Thank you for that!
I have a question:
I want to do that on my E-commerce site. I have a lot of categories and I want that just in this one category it will require 4 units min (lets call it "66").
When I put this code its require to buy at least 4 units from a category "66". even when someone wants to buy from a different category it is required to buy from category "66".
Can you help me please?
Thanks
@bekarice thank you for solution!
function sww_check_category_for_minimum() {
// set the minimum quantity in the category to purchase
$min_quantity = 3;
// set the id of the category for which we're requiring a minimum quantity
$category_id = 89;
// get the product category
$product_cat = get_term( $category_id, 'product_cat' );
$category_name = '<a href="' . get_term_link( $category_id, 'product_cat' ) . '">' . $product_cat->name . '</a>';
// get the quantity category in the cart
$category_quantity = sww_get_category_quantity_in_cart( $category_id );
if ( $category_quantity < $min_quantity ) {
// render a notice to explain the minimum
wc_add_notice( sprintf( 'Sorry, you must purchase at least %1$s products from the %2$s category to check out.', $min_quantity, $category_name ), 'error' );
}
}
add_action( 'woocommerce_check_cart_items', 'sww_check_category_for_minimum' );
/**
-
Returns the quantity of products from a given category in the WC cart
-
@param int $category_id the ID of the product category
-
@return in $category_quantity the quantity of products in the cart belonging to this category
*/
function sww_get_category_quantity_in_cart( $category_id ) {// get the quantities of cart items to check against
$quantities = WC()->cart->get_cart_item_quantities();// start a counter for the quantity of items from this category
$category_quantity = 0;// loop through cart items to check the product categories
foreach ( $quantities as $product_id => $quantity ) {$product_categories = get_the_terms( $product_id, 'product_cat' ); // check the categories for our desired one foreach ( $product_categories as $category ) { // if we find it, add the line item quantity to the category total if ( $category_id === $category->term_id ) { $category_quantity += $quantity; } }
}
return $category_quantity;
}
I have added this to function.php.
If I have less than 3 products from green grocery (category id- 89) it shows an error but let's say I don't want to buy 3rd item, instead I remove two items from green grocery category but when I remove that category products, the error still stays there - https://prnt.sc/115nxlu
How do I resolve tyis ?
I am trying to use this for a restaurant that does catering for a minimum of 25 people, and customers also have the ability to order regular carry-out menu items. Even with the category ID in there it still wont let non-category items to be ordered unless the quantity is 25. Any advice? You can contact me at clopton78@gmail.com