过滤自定义产品循环以从WooCommerce中的某个类别中获取产品



我有一个函数,它调用我所有的产品类型"promotion_package";WooCommerce:

<?php foreach ( (array) $products as $product ):
if ( ! $product->is_type( 'promotion_package' ) || ! $product->is_purchasable() || $product->get_duration() <= 0 ) {
continue;
}
endforeach; ?>

我只想从";vip";产品类别。

我尝试使用CCD_;vip";类别,但没有起作用。

我怎样才能只展示";vip";类别

您可以尝试'product_cat'产品类别分类法的has_term()条件函数,如下所示:

<?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; 
?>

它应该起作用,

最新更新