Dynamically assign a custom product template for the out of stock products

Solved
Tourist
18 0 0

Hello, 

Can  a shopify expert please help me with the below explained issue?

I have used the following tutorial to create a new template to be used for out of stock products so they can contact the merchant if they want to preorder/get more info regarding out of stock product etc.

https://help.shopify.com/en/themes/customization/products/add-to-cart/replace-add-to-cart-button

 

The tutorial says at the end that i can go to each product individually and select the template i want.

Is there a way to dynamically assign this new "product-template-requires-contact" template where a part is out of stock?

This would update all the current parts and any future parts when inventory drops to zero automatically changes its current template to this new "product-template-requires-contact" template.

0 Likes

Success.

Astronaut
884 105 197

While you can use a new template to do this, you can also just do it in your current product template. So in your regular product-template file, instead of commenting out {% include 'product-form' %} wrap an if statement around it:

 

{% if product.available %}
{% include 'product-form' %}
{% else %}
<p>Please <a href="mailto:{{ shop.email }}">contact us</a> if you are interested in this product.</p>
{% endif %}
If you'd like to make any edits to your store, please send me a personal message and we can discuss what you'd like to accomplish :D
1 Like
Shopify Partner
462 80 94

@devumi 

 

sarhov is here...

 

In shopify we can preview some template adding this line at the end of the current address ?view=template-name

The trick is the following, your are checking product availability via liquid and if it is not available, you redirect it into that view.

So, open your theme.liquid file and right before closing </head> tag, past the following code.

{% assign productTemplateName = 'alternate' %}
{% unless template.suffix == productTemplateName %}
{% if template.name == 'product' %}
{% unless product.available %}
<script>
document.location = '{{product.url}}?view={{productTemplateName}}'
</script>
{% endunless %}
{% endif %}
{% endunless %}

make sure you are changed template name in code into yours. on this part of the code, in this case it assumes the product template name is product.alternate

 

{% assign productTemplateName = 'alternate' %}
Author: Hovhannes Sargsyan (aka sarhov).
An individual web developer focused on Shopify development.
Need more help? Feel free to send me a direct message or drop an email shopify@sarhov.com

Website: https://sarhov.com  
Email: shopify@sarhov.com
Skype: @sarhov
Messenger: @sarhov
Telegram: @sarhov
0 Likes
Tourist
18 0 0

Hello @Ninthony,

 

Thank you so much for guiding in the right path.

I have one question though , in your code below, if the product is available, it should include the product-template and if the product is unavailable,it should include the product form,right? did you mean to have 'product-template' in place of product-form when the if condition passes? 

{% if product.available %}
{% include 'product-form' %}
{% else %}
<p>Please <a href="mailto:{{ shop.email }}">contact us</a> if you are interested in this product.</p>
{% endif %}

 i have adjusted the code as follows

{% if product.available %}
       {% section 'product-template' %}
{% else %}
       {% section 'product-template-requires-contact' %}
{% endif %}

0 Likes
Tourist
18 0 0

thank you for replying, when i tried your code it  caused a loop when i visited the product page and didn't render the page.  there must be some little tweak needed.

0 Likes
Shopify Partner
462 80 94

@devumi 

 

The loop issue can be if the template name is wrong... saying template name I mean template and not section, those are complete difference ones.

Author: Hovhannes Sargsyan (aka sarhov).
An individual web developer focused on Shopify development.
Need more help? Feel free to send me a direct message or drop an email shopify@sarhov.com

Website: https://sarhov.com  
Email: shopify@sarhov.com
Skype: @sarhov
Messenger: @sarhov
Telegram: @sarhov
1 Like
Tourist
18 0 0

i did give the right template name and just double checked to make sure it was not a section name and that was indeed the case.

0 Likes
Highlighted
Shopify Partner
462 80 94

@devumi 

 

Yep, it's strange it works at my end without any issue, maybe you can add me as collaborator and I can look the source ?

Author: Hovhannes Sargsyan (aka sarhov).
An individual web developer focused on Shopify development.
Need more help? Feel free to send me a direct message or drop an email shopify@sarhov.com

Website: https://sarhov.com  
Email: shopify@sarhov.com
Skype: @sarhov
Messenger: @sarhov
Telegram: @sarhov
0 Likes