[英]Filter a custom product loop to get products from a category in WooCommerce
I have this function which calls me all products type "promotion_package" in WooCommerce:我有这个 function,它称我为 WooCommerce 中的所有产品类型“promotion_package”:
<?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.我只想从“vip”产品类别中获得产品。
I tried using $product->get_categories()
to find "vip" category, but it didn't worked.我尝试使用
$product->get_categories()
来查找“vip”类别,但没有奏效。
How can I show only products from "vip" category?如何仅显示“vip”类别的产品?
You can try has_term()
conditional function for 'product_cat'
procuct category taxonomy as follows:您可以尝试
has_term()
条件 function 用于'product_cat'
产品类别分类,如下所示:
<?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,它应该工作,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.