【问题标题】:Hide from all site and single page product of a whole specific category in WooCommerce在 WooCommerce 中隐藏整个特定类别的所有站点和单页产品
【发布时间】:2020-08-21 21:52:38
【问题描述】:

我一直在尝试使用this StackOverFlow answer code 并按照this other forum thread 上的说明隐藏特定类别(从商店页面和单个页面)中的所有产品

Exclude specific product categories on Woocommerce single product pages 回答代码上,我已将我的产品类别之一定义如下 (此处为术语 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;
}

任何帮助将不胜感激。

【问题讨论】:

  • 嗨!代码运行良好。是我激活的插件有问题。我的解决方案有两个问题:1)类别过滤器仍然显示隐藏类别。 2)我一直在使用 WP Bakery 中用于 woocommerce 的添加到购物车预设按钮,并且使用此代码,该按钮未显示。是否有额外的代码来启用它?或以其他方式获取此特定类别产品的“添加到购物车”按钮?非常感谢提前
  • 大家好,我仍然遇到这个问题。首先,我不能将 WP Bakery 的添加到购物车按钮用于 woocommerce,似乎隐藏类别禁用了此选项。其次,我也无法在商店选项中隐藏类别。是否可以使用上面的代码启用此添加到购物车选项?之后有可能隐藏特定类别吗?非常感谢,并希望在此问题上获得最后的帮助。

标签: php wordpress woocommerce product taxonomy-terms


【解决方案1】:
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']);

    }
}

【讨论】:

  • 您好,感谢您的快速回复。我删除了所有我用过的代码并添加了这个,但没有用。我将代码更改为 $category_ids = array( 43 );它也没有工作。有什么我可能做错了吗?
猜你喜欢
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 2018-07-07
  • 2019-06-23
  • 2021-09-16
  • 2022-01-12
  • 2018-04-23
相关资源
最近更新 更多