Created
December 9, 2015 08:00
-
-
Save bekarice/2061fcbdc3ad2b80ddf8 to your computer and use it in GitHub Desktop.
WooCommerce Require Category Minimum for Purchase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function sww_check_category_for_minimum() {
}
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 ) {
}
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 ?