I have a product with different variations. To choose an variation two select fields have to be filled. However, the availability message is only shown when both select fields have a option selected. I want to show a general availability message as long as not variation is selected: "Choose location and date to see availability".
I know how to change the message with the "woocommerce_get_availability_text"-filter. But I don't know how to present this message right from the page load and which part of WooCommerce's JavaScript I have to change in order to make it work.
I think the following is necessary:
Adding
<div class="woocommerce-variation-availability">Choose location and date to see availability</div>to content-single-product.php, so that the general message is shown right at the page load.Add some JS which puts the
in-stockandout-of-stockmessages to the div as soon as a variation is selected. The JS has also to take care that the general message gets display again as soon as no variation is selected anymore.
To show a custom text on variable products when no variation is selected use the following:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
To target specific variable products only, replace in the function:
with:
Edit:
As the existing parent
<div class="woocommerce-variation single_variation">for<div class="woocommerce-variation-availability">child element, get hidden (slideDown usage by WooCommerce), when there are no variations selected you can not use it to display your custom div inside<div class="woocommerce-variation-availability">.It's not really possible to change the behavior slideUp / slideDown that WooCommerce is doing on
<div class="woocommerce-variation single_variation">, without editing the WooCommerce JS files.Here is a revisited code version using a
<div>container andslideUp()/slideDown()methods instead ofshow()/hide()methods:Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
This doesn't sole the problem as you would like in your comment, but it uses the same behavior as WooCommerce.