Coupons and Discounts

Filters available for WPPizza Coupons and Discounts

wppizza_cad_filter_discounts

Allow to filter (unset) applied discounts – identified by their ID – depending on custom conditions related to cart contents. As with all customisations, use at your own risk. You should test this thoroughly

/*
@since 2.1.4
@param array
@param array
@return array

EXAMPLE:
Remove a specific applied simple discount - identified by its id - if cart contains an item from a certain category
*/
add_filter('wppizza_cad_filter_discounts', 'myprefix_filter_wppizza_cad_filter_discounts', 10, 2);
function myprefix_filter_wppizza_cad_filter_discounts($array_of_discount_ids, $cart){
	/*
		skip if there isn't anything in the cart to start off with
	*/
	if(empty($cart)){
		return $array_of_discount_ids;
	}

	/*
		discount id to remove if cat id in cart
	*/
	$discountIdToRemove = 1;	
	//skip if it does not exist in array anyway
	if(empty($array_of_discount_ids[$discountId])){
		return $array_of_discount_ids;
	}
		
	/*
		check for category id used by an item in cart
	*/
	$findCatId = 6;
	
	/*
		array keys of items consist of blogid.categoryid.postid.sizes and more
		
		Note:
		there are also many other ways of doing this by iterating through all items or similar....
		whatever works for you
	*/
		
	//get all keys cart 
	$cart_item_keys = array_keys($cart);
	
	//iterate through keys to get category id's
	foreach($cart_item_keys as $cart_item_key){
		
		//explode keys by '.'
		$x = explode('.',$cart_item_key);
		
		/* 
			Example: 
			if one of the items in the cart belongs to cat id = 6 , 
			remove discount with id 1 

			$x[0] => blog id
			$x[1] => cat id
			$x[2] => post id
			$x[3] => sizes id
			$x[4] => size id
			.....
		*/
		if($x[1] == $findCatId){
			unset($array_of_discount_ids[$discountIdToRemove]);
			break;//no need to continue iteration now	
		}
	}
	
return $array_of_discount_ids;
}