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.