Ecommerce Shopify WordPress Discussion

Auto define default selected attribute values on WooCommerce variable products

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)
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.

February 17, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.