Created
February 15, 2022 17:23
-
-
Save wpflames/d1903b025a63a5730ae4ffd576399654 to your computer and use it in GitHub Desktop.
Check if specific category is in the cart
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
| <?php | |
| // ========================================================================= | |
| // CHECK IF SPECIFIC CATEGORY IS IN THE CART | |
| // ========================================================================= | |
| function check_specific_category_in_cart() { | |
| // Set $cat_in_cart to false | |
| $cat_in_cart = false; | |
| // Loop through all products in the Cart | |
| foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
| // If Cart has category "flower", set $cat_in_cart to true | |
| if ( has_term( 'flower', 'product_cat', $cart_item['product_id'] ) ) { | |
| $cat_in_cart = true; | |
| break; | |
| } | |
| } | |
| // Do something if category "flower" is in the Cart | |
| if ( $cat_in_cart ) { | |
| echo 'There is flower in the cart!'; | |
| } | |
| } | |
| add_action( 'woocommerce_before_cart', 'check_specific_category_in_cart' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment