7

I would like to remove from the dropdown of variations in WooCommerce product page following "option": WooCommerce "Select an option" issue

I found plenty of, apparently not working codes which should do the job. Probably outdated to the latest WooCommerce version.

What I tried and is partially working:

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'my_wc_filter_dropdown_args', 10 );
function my_wc_filter_dropdown_args( $args ) {
    $args['show_option_none'] = '';
    return $args;
}

This is only working when I set some text between '', not empty. When it's added into functions.php exactly like above, it's without change and set to default text - "Select an option" like on the picture. I am not sure what's wrong here. I also tried "false" or "none" but didn't work with either option.

If anyone could help me with this I would be grateful.

I am using latest WP 4.9.6 and latest of WooCommerce (whatever version it is). Everything is updated to the latest version, even PHP (7.2).

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Roman Bartík
  • 67
  • 1
  • 2
  • 7

3 Answers3

14

The correct way to do it is to use woocommerce_dropdown_variation_attribute_options_html filter hook instead. Below the screenshot for normal variable product with default attribute dropdowns:

enter image description here

So there is 2 different case:

1) Removing this html option completelly**:

add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'filter_dropdown_option_html', 12, 2 );
function filter_dropdown_option_html( $html, $args ) {
    $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
    $show_option_none_html = '<option value="">' . esc_html( $show_option_none_text ) . '</option>';

    $html = str_replace($show_option_none_html, '', $html);

    return $html;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

The html option is removed completely, keeping only option with product attribute values:

enter image description here


2) Remove only the text "Select an option" (you will have an option without label name):

add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'filter_dropdown_option_html', 12, 2 );
function filter_dropdown_option_html( $html, $args ) {
    $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
    $show_option_none_text = esc_html( $show_option_none_text );

    $html = str_replace($show_option_none_text, '', $html);

    return $html;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

All code is tested on latest Woocommerce version 3.4.x

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you, this is indeed removing the text "Choose an option". But the way it would look and work better would be to remove the line entirely. Now it looks like this: https://i.imgur.com/qjf6SJF.png Is it possible to remove it like that? Thanks in advance. – Roman Bartík Jul 04 '18 at 23:28
  • @RomanBartík There is 2 code snippets: The first one **remove the html option line completely**, just as you ask… The second one remove just the text. Try the First one, you will see it works on Woocommerce last version. – LoicTheAztec Jul 04 '18 at 23:40
  • Sorry, I didn't notice your screenshots as page loaded without them so it was little confusing. Everything works as you said. I need the first version you posted so I used that, but both works as you are saying. Thanks again and sorry for missunderstanding. – Roman Bartík Jul 05 '18 at 00:06
  • @LoicTheAztec I'm having a problem trying to make case 1 work, it only works partially, the choose an option text is removed and first real option is selected correctly but the issue is that the text is also removed from this option. Using the latest woocomerce version – Richard Mišenčík Nov 13 '18 at 12:00
  • @RichardMišenčík I have tested the code on WC last version and it still word perfectly. It might be related to your language I suppose… – LoicTheAztec Nov 13 '18 at 14:15
  • @LoicTheAztec Oh that might be it, but shouldn't it work anyway since the code is handling translated strings as well? `__( 'Choose an option', 'woocommerce' )`. Because it correctly gets rid of the default option but it also removed the option after that. – Richard Mišenčík Nov 18 '18 at 20:44
  • @LoicTheAztec Can we use your snippet to remove another option as well? Let's say I want to remove the "Black" option in your example above? – Onur Jun 06 '21 at 17:43
  • @LoicTheAztec Ohh just found that you've already answered my question here: https://stackoverflow.com/a/54987217/4131419 Great, thanks! Sorry for bothering :) – Onur Jun 06 '21 at 17:56
3

Add this code to function.php file of your current theme.

add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'filter_dropdown_option_html', 12, 2 );
function filter_dropdown_option_html( $html, $args ) {
    $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
    $show_option_none_html = '<option value="">'.esc_html( $show_option_none_text ).'</option>';
    $html = str_replace($show_option_none_html, '', $html);
    return $html;
}
TheMisir
  • 4,083
  • 1
  • 27
  • 37
loudbel
  • 51
  • 4
2

Just put in function file

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'wc_remove_options_text');
function wc_remove_options_text( $args ){
    $args['show_option_none'] = '';
    return $args;
}