Skip to content
Advertisement

How can I show individual stock quantities for variable products on the product page in WooCommerce

I am trying to show each variable products stock quantity on the single product page in WooCommerce.

I have managed to show all the stock quantities for all the variations using the below code from Rodolfo Melogli of Business Bloomer but I would ideally like to only show a single stock quantity when each size option is selected.

Attached is a screenshot to explain it better.

enter image description here


The html output of the buttons:

  • <span class="ivpa_term ivpa_active ivpa_instock" data-term="l">L</span>

or when clicked

  • <span class="ivpa_term ivpa_active ivpa_clicked ivpa_instock" data-term="xl">XL</span>

As you can see it shows all the variations but I would just like to show the selected variations (S, M, L, XL etc.) quantities as highlighted in red.


Here is the PHP code:

JavaScript

Advertisement

Answer

  • The first part of the code is for testing purposes only, specific to your theme where I’m going to mimic the buttons
  • At the output of the variations, some extra classes are provided, to determine the difference in code between each variation
  • With the jQuery piece all variations are hidden, then on the basis of the class + data term (L, XL, etc…) the selected variation is displayed after clicking on a certain button

This part is only for debugging purposes, to imitate the buttons that are present in your theme, this code may be removed (after testing)

JavaScript

Shows all the variations

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement