在 WooCommerce 变量产品下拉列表中隐藏特定的产品属性术语 [英] Hide specific product attribute terms on WooCommerce variable product dropdown

查看:80
本文介绍了在 WooCommerce 变量产品下拉列表中隐藏特定的产品属性术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想隐藏一个包含所有值的属性.请查看链接.https://csepromos.nl/product/oregon-400-ml-drinkfles-met-karabijnhaak/

I would like to hide one attribute with all the values. Please see the link. https://csepromos.nl/product/oregon-400-ml-drinkfles-met-karabijnhaak/

我想隐藏产品页面上的以下属性和以下值:

I would like to hide the below attribute and the below values on the product page:

Filter kleur:Grijs Groen Oranje Paars Rood Wit Zwart

Filter kleur: Grijs Groen Oranje Paars Rood Wit Zwart

这可以用代码完成吗?

我发现下面的代码有点帮助,但我没有下拉菜单,你怎么能一次隐藏所有的值?

I found the below code which helps a little bit but i do not have a dropdown menu and also how can you hide all the values at once?

https://stackoverflow.com/a/54987217/13407118

谢谢!

推荐答案

您可以使用以下示例基于 "Color" 产品属性,其中 2 个值要从变量的属性下拉列表中删除产品.

You can use the following example based on "Color" product attribute with 2 values to be removed from the attribute dropdown of a variable product.

您需要设置产品属性分类,始终以 pa_ 开头,后跟产品属性 slug.所以对于颜色"产品属性,分类是pa_color.

You will need to set the product attribute taxonomy, that always start by pa_ followed by the product attribute slug. So For "Color" product attribute, the taxonomy is pa_color.

然后您将在数组中设置您想要的产品属性术语名称(您想从下拉列表中隐藏的名称.

Then you will set in your desired product attribute term names in an array (the ones you want to hide from the dropdown.

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_dropdown_variation_args', 10, 1 );
function filter_dropdown_variation_args( $args ) {
    // HERE set your product attribute taxonomy (always start with "pa_" + the slug
    $taxonomy = 'pa_color';

    // HERE set the product attribute terms names that you want to hide
    $targeted_terms_names = array( "Blue", "Red" );


    // Convert term names to term slugs
    $terms_slugs = array_filter( array_map( 'sanitize_title', $targeted_terms_names ) );

    // Targeting a product attribute only
    if( $args['attribute'] === $taxonomy ) {
        // Loop through the product attribute option values
        foreach( $args['options'] as $key => $option ){
            if( in_array( $option , $terms_slugs ) ) {
                unset($args['options'][$key]);
            }
        }  
    }

    return $args;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

请注意,您无法从下拉列表中隐藏包含所有术语值的产品属性,因为客户将无法选择任何产品变体.

Note that you can't hide a product attribute with all term values from the dropdown as customer will not be able to select any product variation.

这篇关于在 WooCommerce 变量产品下拉列表中隐藏特定的产品属性术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆