Created
January 20, 2023 07:56
-
-
Save ahmedMshaban/c0a11263441f65743c6bef8a1e725c02 to your computer and use it in GitHub Desktop.
Handle products and product in categories that can not be sold or shipped with anything else
This file contains hidden or 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
/** | |
* Description: Handle products and product in categories that can not be sold or shipped with anything else | |
*/ | |
add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_to_cart_validation_filter_callback', 10, 3 ); | |
function wc_add_to_cart_validation_filter_callback( $passed, $product_id, $quantity ) { | |
if( WC()->cart->is_empty() ) | |
return $passed; | |
// List of product Ids that can not be sold with anything else | |
$products_can_not_be_sold_with_anything_else = array(53048); | |
// List of categories that can not be sold with anything else | |
$categories_can_not_be_sold_with_anything_else = array( | |
'chukar', | |
'quick-chicks', | |
'game-birds', | |
'pheasants', | |
'hatching-eggs', | |
'special-deals', | |
'started-pullets', | |
'15-20-week-old-started-pullets', | |
'supplies', 'chick-supplies', | |
'coop-supplies', | |
'shipping-supplies', | |
'feeders-waterers', | |
'gift-cards', | |
'health-wellness' | |
); | |
// Loop through cart items | |
foreach( WC()->cart->get_cart() as $cart_item ) { | |
// Check on category level | |
// Check the product that I need to add if it can not be purashed with anything else | |
if (has_term( $categories_can_not_be_sold_with_anything_else, 'product_cat', $product_id )) { | |
wc_add_notice( | |
sprintf( | |
__("This product cannot be purchased together with anything else.") | |
), 'error'); | |
return false; | |
} | |
// check if cart contain a product that can not be purashed with anything else | |
else if (has_term( $categories_can_not_be_sold_with_anything_else, 'product_cat', $cart_item['product_id'] )) { | |
wc_add_notice( | |
sprintf( | |
__("This product cannot be purchased together with %s."), | |
'"<strong>' . $cart_item['data']->get_name() . '</strong>"' | |
), 'error'); | |
return false; | |
} | |
// Check on product level | |
foreach ($products_can_not_be_sold_with_anything_else as $product_can_not_be_sold) { | |
// Check the product that I need to add can not be purashed with anything else | |
if ($product_id == $product_can_not_be_sold) { | |
wc_add_notice( | |
sprintf( | |
__("This product cannot be purchased together with anything else.") | |
), 'error'); | |
return false; | |
} | |
// check if cart contain a product that can not be purashed with anything else | |
else if ($cart_item['product_id'] == $product_can_not_be_sold) { | |
wc_add_notice( | |
sprintf( | |
__("This product cannot be purchased together with %s."), | |
'"<strong>' . $cart_item['data']->get_name() . '</strong>"' | |
), 'error'); | |
return false; | |
} | |
} | |
} | |
return $passed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment