'Automatically pre-select first in stock value in WooCommerce variable product dropdown
As you can tell from the title I try to "automatically pre select the first in stock value in WooCommerce variable product dropdown".
Currently, I'm using the code below to pre-select the first variant, but I need this code to select the first IN STOCK variant. Any advice?
function fun_select_default_option( $args ) {
// Check the count of available options in dropdown
if ( count($args['options']) > 0 ) {
$args['selected'] = $args['options'][0];
}
return $args;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'fun_select_default_option', 10, 1 );
Solution 1:[1]
The callback function only contains $args, via $args['product'] however, you have access to the product variable object.
Based on that you can loop through the visible children. By the variation ID's you can get the product variation object(s).
With the use of get_stock_status() you can then determine the status.
So you get:
function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {
// Check the count of available options in dropdown
if ( count( $args['options'] ) > 0 ) {
// Initialize
$option_key = '';
// Get WC_Product_Variable Object
$product = $args['product'];
// Is a WC Product Variable
if ( is_a( $product, 'WC_Product_Variable' ) ) {
// Loop through children
foreach ( $product->get_visible_children() as $key => $variation_id ) {
// Get product variation object
$variation = wc_get_product( $variation_id );
// Is a WC Product Variation
if ( is_a( $variation, 'WC_Product_Variation' ) ) {
// Get stock status
$product_stock_status = $variation->get_stock_status();
// In stock
if ( $product_stock_status == 'instock' ) {
// Set key
$option_key = $key;
// Break
break;
}
}
}
}
// Finds whether a variable is a number
if ( is_numeric( $option_key ) ) {
// Selected
$args['selected'] = $args['options'][$option_key];
}
}
return $args;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
Alternatively you can also use get_available_variations() and $variation['is_in_stock'].
The big difference with the above answer is that this answer will also see backorders, where backorder is allowed as instock because it does not take the specific stock status into account.
So then you get:
function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {
// Check the count of available options in dropdown
if ( count( $args['options'] ) > 0 ) {
// Initialize
$option_key = '';
// Get WC_Product_Variable Object
$product = $args['product'];
// Is a WC Product Variable
if ( is_a( $product, 'WC_Product_Variable' ) ) {
// Get an array of available variations for the current product
foreach ( $product->get_available_variations() as $key => $variation ) {
// Is in stock
$is_in_stock = $variation['is_in_stock'];
// True
if ( $is_in_stock ) {
// Set key
$option_key = $key;
// Break
break;
}
}
}
// Finds whether a variable is a number
if ( is_numeric( $option_key ) ) {
// Selected
$args['selected'] = $args['options'][$option_key];
}
}
return $args;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 7uc1f3r |
