Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

php - Hide a variation attribute value in Woocommerce variable product dropdown

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; 
}

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

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;}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...