问题
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!
回答1:
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.
来源:https://stackoverflow.com/questions/61598810/hide-specific-product-attribute-terms-on-woocommerce-variable-product-dropdown