1

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

Can this be done with code?

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

Thanks!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Kees Kaas
  • 51
  • 5

1 Answers1

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.

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

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks, this works. However just like you stated in your answer, the products will not be available if you hide all the values in the third attribute. I added the third attribute to have simple color descriptions. The new problem that arises is that the product is not available since the third attribute Filter kleur is hidden and cannot be selected. Is it possible to have the third attribute auto selected when you select the second attribute value? For instance applegroen (applegreen) has variations with all the values of Kies aantal drukkleurren but only with the third attribute groen (green) – Kees Kaas May 05 '20 at 12:24
  • @KeesKaas Yes this is what I have told you at the end of my answer… – LoicTheAztec May 05 '20 at 12:42