I have this function which calls me all products type “promotion_package” in WooCommerce:
JavaScript
x
<?php foreach ( (array) $products as $product ):
if ( ! $product->is_type( 'promotion_package' ) || ! $product->is_purchasable() || $product->get_duration() <= 0 ) {
continue;
}
endforeach; ?>
I would like to get only products from “vip” product category.
I tried using $product->get_categories()
to find “vip” category, but it didn’t worked.
How can I show only products from “vip” category?
Advertisement
Answer
You can try has_term()
conditional function for 'product_cat'
procuct category taxonomy as follows:
JavaScript
<?php
$categories = array('vip'); // Your categories (can be terms Ids, slugs or names)
foreach ( (array) $products as $product ):
if ( ! $product->is_type( 'promotion_package' ) || ! $product->is_purchasable() || $product->get_duration() <= 0
|| ! has_term( $categories, 'product_cat', $product->get_id() ) ) {
continue;
}
endforeach;
?>
It should work,