从单个产品元部分删除特定的 WooCommerce 类别

问题描述 投票:0回答:1

我发现的每个解决方案本质上都是从产品中取消分配类别。这不是我想要的。 我正在使用产品类别为其他插件提供功能。就我而言,“预购”类别的产品使用 YITH 插件申请押金,“配件”类别的产品有一个额外的复选框,供客户建议是否需要产品配件服务的报价。

我想要实现的是从“元”部分的单个产品页面中删除特定显示的类别术语。客户不需要知道“预购”和“试穿”等类别的存在,我也不希望他们能够选择超链接来查看类别页面。是的,我知道该页面仍然可用,但风险很小。

网站上的 HTML 输出如下所示:

<div class="product_meta">
    <span class="sku_wrapper">SKU: <span class="sku">...</span></span>
    <span class="posted_in">
            Categories: 
            <a href="URL" rel="tag">Fitting</a>
            , 
            <a URL" rel="tag">Category 1</a>
            , 
            <a href="URL" rel="tag">Category 2</a>
            , 
            <a href="URL" rel="tag">Preorder</a>            
    </span>
</div>

有没有办法使用 PHP 来做到这一点?

如果没有,我也一直在尝试 jQuery 解决方案,但我也很挣扎,因为我无法选择前面的逗号,它位于

a
标签之外,但位于父级
span
内。

我编写的所有代码最终在前端看起来像这样:

Categories: , Category 1,,Category 2,
未删除逗号的地方。

如有任何帮助,我们将不胜感激!

php wordpress woocommerce product taxonomy-terms
1个回答
0
投票

以下代码将删除单个产品元部分中显示的特定定义的产品类别术语:

add_filter( 'get_the_terms', 'filter_specific_product_categories', 10, 3 );
function filter_specific_product_categories( $terms, $post_id, $taxonomy ) {
    global $woocommerce_loop;
    
    if ( isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) 
    && isset($woocommerce_loop['total']) && $woocommerce_loop['total'] == 0 
    && isset($woocommerce_loop['loop']) && $woocommerce_loop['loop'] == 1 
    && $taxonomy === 'product_cat' ) {
        $targeted_slugs = array('preorder', 'fitting');

        // Loop through the terms
        foreach ( $terms as $key => $term ) {
            if ( in_array( $term->slug, $targeted_slugs ) ) {
                unset($terms[$key]); // Remove WP_Term object
            }
        }
    }
    return $terms;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.