I’m trying to make it so that when the inventory levels for certain products are less than 1, it changes the button to say "Made to Order" instead of "Add to Cart" so that it implies they won’t ship immediately.
I made a new product template and product form and everything associated with that, but when I change the button code, it doesn’t seem to do anything.
I’m using the most recent version of the Palo Alto theme.
<button type="submit" name="add" id="AddToCart--{{ section.id }}" class="btn" data-add-to-cart>
<span id="AddToCartText" data-add-to-cart-text>
{%- if variants_count < 1 -%}
{{- 'products.product.made_to_order' | t -}}
{%- else -%}
{{- 'products.product.add_to_cart' | t -}}
{%- endif -%}
</span>
{%- render 'icon-loading' -%}
</button>
2
Answers
You probably want to issue a little code to tell you the value of the liquid variable variants_count. It is not at all clear what that is set to. Is it the quantity of the current variant in inventory? If so, then your code should work. If not, why are you using it as a check? You can always access the quantity of a variant using the current variant in your variant rendering loop. variant.inventory_quantity is the value you probably should be using.
So the first thing you are probably not doing right is checking for the quantity of a variant outside the product.variants rendering loop. The Add to Cart button is most often outside the scope of this loop, as it usually pertains to adding a product to the cart using a form submit.
So now your challenge is instead to listen to the variant currently selected by the customer. When that variant is selected, the quantity will be used to decide what to print on the button. So you need some Javascript to alter the button text.
Does that make sense? Changing the text once is never going to work unless you only ever have one variant, in which case, checking quantity would work, but as I said, this is a rare thing for most shops, to only ever have one variant.
Try this, code is not tested;
Basically, added
data-qty
when inventory is less than 1. When that option is selected, use js to check the attr and change the text of the button.