Search code examples
phpwordpresswoocommercedropdowntaxonomy-terms

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

Solution

  • 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.