Created
February 16, 2022 11:52
-
-
Save wpflames/01158b903532d60d52ac258bc40910ae to your computer and use it in GitHub Desktop.
Check if multiple categories 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 THERE IS MULTIPLE CATEGORY IN THE CART | |
| // ========================================================================= | |
| function custom_msg_to_specific_cats(){ | |
| $flower_in_cart = false; | |
| $gift_in_cart = false; | |
| // Flower - Loop through all products in the Cart | |
| foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
| if ( has_term( 'flower-delivery', 'product_cat', $cart_item['product_id'] ) ) { | |
| $flower_in_cart = true; | |
| break; | |
| } | |
| } | |
| // Gift - Loop through all products in the Cart | |
| foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
| if ( has_term( 'gift', 'product_cat', $cart_item['product_id'] ) ) { | |
| $gift_in_cart = true; | |
| break; | |
| } | |
| } | |
| // If both condition are true | |
| if ( $flower_in_cart && $gift_in_cart) { | |
| echo 'There are flower and gift categories in the cart' | |
| } | |
| } | |
| add_action('woocommerce_before_cart','custom_msg_to_specific_cats'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment