php - 过滤自定义产品循环以从 WooCommerce 中的类别中获取产品
问题描述
我有这个功能,它在 WooCommerce 中调用我所有产品类型“promotion_package”:
<?php foreach ( (array) $products as $product ):
if ( ! $product->is_type( 'promotion_package' ) || ! $product->is_purchasable() || $product->get_duration() <= 0 ) {
continue;
}
endforeach; ?>
我只想从“vip”产品类别中获得产品。
我尝试使用$product->get_categories()
查找“vip”类别,但没有成功。
如何仅显示“vip”类别的产品?
解决方案
您可以尝试has_term()
以下 procuct 类别分类的条件函数'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;
?>
它应该工作,
推荐阅读
- python-3.x - 如何展平列表中的两个或多个数组?
- java - arg4j 选项但没有参数
- github-actions - 带有 ESP-IDF 编译的 GitHub Actions
- python - 我怎样才能得到之间的文字使用 Selenium 导入 webdriver?
- .net - 如何从 NLOG 中的模板动态添加连接字符串“凭据”
- java - 如何使用 GZipOutputStream 获得中间压缩大小?
- java - Spring Cloud Stream 3.0.7 测试逻辑
- amazon-web-services - Terraform aws:[WARN] 可重试错误:拨打 tcp:在 127.0.0.53:53 上查找 self.public_ip:没有这样的主机
- python - 如何从pdf文件中提取几行?
- c# - 如何按比例拍摄图片框的屏幕截图