Auto Change template based on inventory

Farhan2
New Member
1 0 0

I am looking for a way to adjust the code of the products so that when an item reaches 0 inventory the template will auto change to the pre-order template instead of the regular template. This would need to be vice-versa so that when I have the item back in inventory it would return back to the normal template. I assume that it can be done with an if statement, but not for sure on the correct syntax. 

0 Likes
PaulNewton
Shopify Partner
2588 134 445

Can you clarify if your trying to use a themes alternate template ( liquid templating) 

, OR trying to use modify a shopify plus checkout script template at run time?

For checkout scripts see the shopify-scripts repo for examples, If your just trying to customize your theme this is the wrong subforum , need a move.

 

For liquid syntax is similar to the following with your own conditions, 

{% if condition %}

{% endif %}

To change a template dynamically to an alternate template you have to use the view parameter in the url set to the name of the alternate template example: myshopify.com/product/productname?view=preorder for alternate template named product.preorder.liquid

So you will need to use either a meta refresh in your html's head(theme.liquid) or do a javascript redirect to that url after the product page loads. This is the path to figure out if you have lots of product templates that look different.

Simpler methods:

For example in your product.template you could wrap your normal product logic with a condition to show the preorder code and skip needing alternate templates 

{% if product.available %}
 show preorder version of code...
{% else %}
 show regular code...
{% endif %}

results will vary by theme.

 

Another option is make the preorder code a liquid snippet (/snippets/preorder.liquid) and conditionally use that instead of the product.template by changing theme.liquid 's logic for content_for_layout

{% if condition %}
 {% include 'preorder' %}
{% else %}
{{ content_for_layout }}
{% endif %}

 

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
0 Likes