Last active
April 17, 2016 12:11
-
-
Save hiro288/2c7188e32b83b54537f118e0dfd43d1d to your computer and use it in GitHub Desktop.
Check if any product in cart has a certain Category
This file contains 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 | |
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