php - 在 WooCommerce 中隐藏整个特定类别的所有站点和单页产品
问题描述
我一直在尝试使用此 StackOverFlow 答案代码并按照其他论坛主题上的说明隐藏特定类别(从商店页面和单页)中的所有产品
在“在 Woocommerce 单一产品页面上排除特定产品类别”答案代码中,我已将我的产品类别之一定义如下(此处为术语 Id 43):
$category_ids = array( 43 );
我只需要将此类别(ID 43)的所有产品添加到购物车并购买。
ID 43“计划”类别的产品之一:https ://mamasmateas.atac.cl/product/plan-personalizado-sin-seguimiento/
我测试过的另一个代码是:
add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );
function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if it is a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'plan' ) ) ) { //pass the slug name here
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
任何帮助将非常感激。
解决方案
add_action( 'woocommerce_shop_loop', 'woocommerce_shop_loop' );
function woocommerce_shop_loop() {
global $product;
$category_ids = array( 20 );
var_dump(has_term( $category_ids, 'product_cat', $product->get_id());
if ( has_term( $category_ids, 'product_cat', $product->get_id() ) ) {
unset($GLOBALS['product']);
}
}
推荐阅读
- python - 如何更改字典的键和值
- ios - 从 ViewController 发送事件到 React-Native
- mysql - mysql数据分片地理
- ansible - 为什么 ansible 模块 win_template 没有看到我的 Windows 文件夹?
- angular - 我有一个formArray,在那个formArray中我只有formControls,而不是formGroup,我怎样才能循环通过html中的控件?
- swift - 从 SwiftUI 视图中访问参与者的隔离状态
- php - Doctrine hydrator 用另一个相关实体覆盖一个相关实体
- python - 如何使用 mplfinance python 在 y 轴上添加 hlines 的值
- html - 如何从对象数组中获取每个项目的详细信息?
- ios-universal-links - 测试通用链接时出现“文件被机器人屏蔽”怎么办?