Only load product variations that are in stock
-
I am trying to randomly select a product variation from the dropdown on single product page in Woocommerce. I have created the following function for this using JavaScript:
if($('select#account option').length > 0){ var accsInStock = []; $('select#account option').each(function() { if( $(this).val().length > 0){ accsInStock.push($(this).val()); console.log("Account" + $(this) + " toegevoegd"); } }); const randomAcc = Math.floor(Math.random() * accsInStock.length); $('select#account option').each(function() { if($(this).val() == accsInStock[randomAcc] ){ $(this).attr('selected', 'selected'); console.log("Account " + accsInStock[randomAcc] + " geselecteerd"); return; } }); }
The problem is that Woocommerce initially populates the select dropdown also with product variations that are not in stock. This causes my function to select a product variation sometimes that is not in stock.
I have tracked down to Woocommerce template called variation.php, and saw that Woocommerce does not check if a product variation is available before populating the select dropdown with all the product variations.
variable.php
<?php defined( 'ABSPATH' ) || exit; global $product; $attribute_keys = array_keys( $attributes ); $variations_json = wp_json_encode( $available_variations ); $variations_attr = function_exists( 'wc_esc_json' ) ? wc_esc_json( $variations_json ) : _wp_specialchars( $variations_json, ENT_QUOTES, 'UTF-8', true ); do_action( 'woocommerce_before_add_to_cart_form' ); ?> <form class="variations_form cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo $variations_attr; // WPCS: XSS ok. ?>"> <?php do_action( 'woocommerce_before_variations_form' ); ?> <?php if ( empty( $available_variations ) && false !== $available_variations ) : ?> <p class="stock out-of-stock"><?php echo esc_html( apply_filters( 'woocommerce_out_of_stock_message', __( 'This product is currently out of stock and unavailable.', 'woocommerce' ) ) ); ?></p> <?php else : ?> <table class="variations" cellspacing="0"> <tbody> <?php foreach ( $attributes as $attribute_name => $options ) : ?> <tr> <td class="label"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></td> <td class="value"> <?php wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, ) ); echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) ) : ''; ?> </td> </tr> <?php endforeach; ?> </tbody> </table>
I have been messing around myself a bit to see if I could make it work, but I failed. I was wondering if anyone would be able to help me to edit this file so the select dropdown only will be populated by product variations that are in stock. Any tips, help or feedback is much appreciated!
- The topic ‘Only load product variations that are in stock’ is closed to new replies.