0

I have a specific dropdown field on variable products on my WooCommerce site that only ever has one option available. Example: the customer chooses the item “type”, then “color”, and finally “part number”, where “part number” dropdown always has just one option.

I want to configure the site to remove the choose an option requirement from the “part number” dropdown and instead, automatically select the single option available.

Is there a way to do this? It seems that WooCommerce needs an option that allows you to auto select a variation attribute when there is only one possible attribute available for the combination.

1

Following code solves the purpose:

add_filter('woocommerce_dropdown_variation_attribute_options_args','fun_select_default_option',10,1);
function fun_select_default_option( $args)
{

    if(count($args['options']) > 0) //Check the count of available options in dropdown
        $args['selected'] = $args['options'][0];
    return $args;
}
  • For some reason that's not having any effect. I've inserted it into the functions.php file of my child theme. Is there anything I need to change to target the specific dropdown field? – wooq123 Nov 19 '18 at 8:37
  • check the result of count($args['options']) – Aparna_29 Nov 19 '18 at 9:24
  • Still can't get it to change the behavior of the dropdowns even when I experiment with different values for the ($args['options']). It still requires me to manually select the option available in the last dropdown. Another way of putting it is that I simply need to remove "choose an option" in all cases and force it to select the first option available. – wooq123 Nov 19 '18 at 9:38
  • What is the value you get on printing count($args['options'])? – Aparna_29 Nov 19 '18 at 9:39
  • Forgive me for not completely understanding, but how do I determine that? Do you mean I need to check the count of available variation combinations on a specific product? – wooq123 Nov 19 '18 at 11:36
0

Refer to the Original answer.

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.