Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created February 16, 2022 11:52
Show Gist options
  • Select an option

  • Save wpflames/01158b903532d60d52ac258bc40910ae to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/01158b903532d60d52ac258bc40910ae to your computer and use it in GitHub Desktop.
Check if multiple categories is in the cart
<?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