问题
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;}
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/54985902/hide-a-variation-attribute-value-in-woocommerce-variable-product-dropdown