Krystian
Krystian

Reputation: 987

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

Upvotes: 2

Views: 6062

Answers (2)

Manik Malhotra
Manik Malhotra

Reputation: 632

Instead of the code, you can disable variation from the Edit Product Page. In the variation tab in product data meta boxes, you can uncheck the enable checkbox. This will remove the dropdown from the front end. Screenshot attached for the same.

enable/disable product variation

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254378

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.

Upvotes: 5

Related Questions