Created
July 26, 2019 13:51
-
-
Save adampmoss/712b716db4f30a38f1b2f48c34bb6161 to your computer and use it in GitHub Desktop.
WooCommerce - check cart before adding item and fail validation if certain items exist
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 | |
/************************************************************************** | |
* CART VALIDATION FOR GOURMET / PACKAGES | |
*/ | |
/** | |
* This function loops through cart items to see if | |
* there are ANY packages in it | |
* | |
* @return bool | |
*/ | |
function does_the_cart_contain_any_packages() | |
{ | |
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { | |
$product = $cart_item['data']; | |
if (is_product_a_package($product)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* This function loops through cart items to see if | |
* there are ONLY packages in it. Empty cart returns | |
* false | |
* | |
* @return bool | |
*/ | |
function does_the_cart_contain_only_packages() | |
{ | |
if (WC()->cart->get_cart_contents_count() == 0) { | |
return false; | |
} | |
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { | |
$product = $cart_item['data']; | |
if (!is_product_a_package($product)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* This function loops through cart items to see if | |
* there are ANY gourmet products in it | |
* | |
* @return bool | |
*/ | |
function does_cart_contain_gourmet_items() | |
{ | |
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { | |
$product = $cart_item['data']; | |
if (is_product_a_gourmet_menu_item($product)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* This function checks if a given product has a package-type SKU | |
* | |
* @param WC_Product $product | |
* @return boolean | |
*/ | |
function is_product_a_package(WC_Product $product) | |
{ | |
return (strpos($product->get_sku(), "EATUP-") !== false); | |
} | |
/** | |
* This function checks if a given product has a gourmet-type SKU | |
* | |
* @param WC_Product $product | |
* @return boolean | |
*/ | |
function is_product_a_gourmet_menu_item(WC_Product $product) | |
{ | |
for ($i=1; $i<7; $i++) { | |
if (strpos($product->get_sku(), "m".$i."-") !== false) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* This function runs as a filter when trying to add an item to the cart. | |
* If gourmet items are in the cart while ordering packages or vice versa | |
* the add to cart attempt will fail validation. | |
* | |
* @param bool $validation | |
* @param int $product_id | |
* @return bool | |
*/ | |
function check_cart_for_packages_or_gourmet_when_adding_to_cart(bool $validation, int $product_id) | |
{ | |
if (WC()->cart->get_cart_contents_count() == 0) { | |
return $validation; | |
} | |
$product = wc_get_product($product_id); | |
if (is_product_a_package($product) && does_cart_contain_gourmet_items()) { | |
wc_clear_notices(); | |
wc_add_notice('Please remove any gourmet meals from your <a href="/cart">shopping cart</a> before adding a package', 'error'); | |
$validation = false; | |
} | |
if (is_product_a_gourmet_menu_item($product) && does_the_cart_contain_any_packages()) { | |
wc_clear_notices(); | |
wc_add_notice('Please remove any packages from your <a href="/cart">shopping cart</a> before adding gourmet meals', 'error'); | |
$validation = false; | |
} | |
return $validation; | |
} | |
add_filter('woocommerce_add_to_cart_validation', 'check_cart_for_packages_or_gourmet_when_adding_to_cart', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment