首页 > 解决方案 > 在 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;
}

任何帮助将非常感激。

标签: phpwordpresswoocommerceproducttaxonomy-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']);

    }
}

推荐阅读