Adding stock status to product variation dropdowns can help improve customer experience and reduce customer support issues. To do this, you will need to edit your theme's template files.
There are two ways to edit your theme's templates:
- Edit the templates directly through the WordPress admin area.
- Edit the templates through an FTP client.
We recommend editing your templates through an FTP client, as this will give you more control over the changes you make.
Once you have access to your theme's template files, you will need to edit the following file:
/wp-content/themes/YOUR-THEME-NAME/template-parts/content-single-product.php
You will need to find the following code:
<?php woocommerce_template_single_add_to_cart( $args ); ?>
And replace it with the following code:
<?php
$args['class'] = implode( ' ', array_filter( array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) );
if ( $product->is_type( 'simple' ) ) {
$args['attributes']['data-product_id'] = $product->get_id();
$args['attributes']['data-quantity'] = ( isset( $args['quantity'] ) ) ? $args['quantity'] : 1;
} elseif ( $product->is_type( 'variation' ) ) {
$args['attributes']['data-product_id'] = $product->get_parent_id();
$args['attributes']['data-variation_id'] = $product->get_id();
}
wc_get_template( 'loop/add-to-cart.php', $args );
?>
This code will allow you to add stock status to product variation dropdowns.