在Woocommerce变量产品下拉菜单中隐藏变体属性值

在Woocommerce变量产品下拉菜单中隐藏变体属性值

问题描述:

我想隐藏特定的变化,使其不显示在woocommerce产品页面的下拉列表中.我能够隐藏选择一个选项",但是我正在为 value ="2,5 kg"

I want to hide specific variations from being shown in the dropdown of woocommerce product pages. I was able to hide the "choose an option" but I am struggling with doing the same for the value="2,5 kg" for example

这是我隐藏选择选项"的代码:

This is my code for hiding "choose an option":

add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'remove_choose_an_option'); 
function remove_choose_an_option( $html ){ 
    $html = str_replace('<option value="">' . __( 'Choose an option', 'woocommerce' ) . '</option>','',$html);
    return $html; 
}

但是如何隐藏例如 value ="2,5 kg" 的变化?示例产品: https://www.keimster.de/produkt/gekeimtes-gesundes-牛奶什锦早餐/

But how to hide the variation with the value="2,5 kg" for example? example product: https://www.keimster.de/produkt/gekeimtes-gesundes-muesli/

我也尝试过使用CSS,但是这2个都不起作用

I also tried with css but none of those 2 is working

#groesse > option[value=2,5 kg], #groesse > option:nth-child(1) {display: none;}

请尝试以下操作,该操作将从单个可变产品页面上的产品属性下拉菜单中隐藏选择选项"并隐藏"2.5千克"选项值:

Try the following instead that will hide "choose an option" and will hide "2,5 kg" option value from product attribute dropdown on single variable product pages:

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_dropdown_variation_args', 10, 1 );
function filter_dropdown_variation_args( $args ) {
    // Dont show "Choose an option"
    $args['show_option_none'] = false;

    // Remove the option value "2,5 kg"
    foreach( $args['options'] as $key => $option ){
        if( $option === "2,5 kg" ) {
            unset($args['options'][$key]);
        }
    }
    return $args;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.应该可以.

Code goes in function.php file of your active child theme (or active theme). It should works.