WooCommerce pre select product attribute

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.