Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created February 15, 2022 17:23
Show Gist options
  • Select an option

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

Select an option

Save wpflames/d1903b025a63a5730ae4ffd576399654 to your computer and use it in GitHub Desktop.
Check if specific category is in the cart
<?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