Skip to content

Instantly share code, notes, and snippets.

@hiro288
Last active April 17, 2016 12:11
Show Gist options
  • Save hiro288/2c7188e32b83b54537f118e0dfd43d1d to your computer and use it in GitHub Desktop.
Save hiro288/2c7188e32b83b54537f118e0dfd43d1d to your computer and use it in GitHub Desktop.
Check if any product in cart has a certain Category
<?php
function card_in_cart() {
global $woocommerce;
$card_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$_categoryid = $term->term_id;
}
if ( $_categoryid === 13 ) {
//a product from 'card' category is in cart!
$card_in_cart = true;
}
}
return $card_in_cart;
}
// usage
if(!card_in_cart()) {
// show message here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment