Auto define default selected attribute values on WooCommerce variable products

113 views Asked by At

In WooCommerce, If I have a product with only one option available, I need to pre select it.

I tried:

add_filter('woocommerce_dropdown_variation_attribute_options_args', 'fun_select_default_option', 10, 1);

function fun_select_default_option($args) {
    if (count($args['options']) === 1) {
        $onlyOption = reset($args['options']);
        if (!isset($onlyOption['disabled']) || !$onlyOption['disabled']) {
            $args['selected'] = $onlyOption;
        }
    }
    return $args;
}

This works if a dropdwown has only one option and that option is available. But not if there's 3 and one available.

So I tried filtering out available options but still does not work:

add_filter('woocommerce_dropdown_variation_attribute_options_args', 'fun_select_default_option', 10, 1);

function fun_select_default_option($args) {
    $selectableOptions = array_filter($args['options'], function($option) {
        return !isset($option['disabled']) || !$option['disabled'];
    });

    if (count($selectableOptions) === 1) {
        $onlyOption = reset($selectableOptions);
        $args['selected'] = $onlyOption;
    }

    return $args;
}

It should pre-select if only one option for attribute is available.

Example:

Product 1

Size: S M L (S L out of stock, M preselected)

Color: Blue (only available, pre selected)

Product 2

Size: S M L (all available, no preselection)

Color: Blue Red (Red available, preselected)

1

There are 1 answers

4
LoicTheAztec On

To auto define default variation attributes for variable products, you can use the following:

add_filter( 'woocommerce_product_get_default_attributes', 'auto_define_default_variation_attributes', 10, 2 );
function auto_define_default_variation_attributes( $default_attributes, $product ){
    $variation_attributes = $product->get_variation_attributes();

    // Loop through available variations
    foreach( $product->get_available_variations('objects') as $variation ) {
        // Target purchasable in stock variation
        if ( $variation->is_purchasable() &&  $variation->is_in_stock() ) {
            // Loop through attributes for the current variation
            foreach( $variation->get_attributes() as $attribute => $value ) {
                if ( ! empty($value) ) {
                    $default_attributes[$attribute] = $value;
                } else {
                    // If there is no defined value, set it
                    foreach( $variation_attributes as $variation_attribute => $values ) {
                        if ( $attribute === sanitize_title($variation_attribute) ) {
                            $default_attributes[$attribute] = current($values);
                            break;
                        }
                    }
                }
            }
            break; // Stop the loop
        }
    }
    return $default_attributes; // Always return the values in a filter hook
}

Code goes on functions.php file of your child theme (or in a plugin). Tested and works.

Note: The code handle WooCommerce product attributes and custom attributes for variations.