How To Express "If this --> this, or if this --> this, otherwise --> this"?

Captain_s_Staff
New Member
4 0 0

I'm modifying how my add-to-cart button displays text based on what collection the product is part of. The theme (Mobilia) has a built-in option to display one alternate button text based on collection. I want to modify it to have two different alternate button texts, but I can't figure out the syntax to express two different variables before the {% else %} tag.

Here's the original code for a single alternate:

      {% capture add_to_cart_label %}
      {% if collection_handles contains 'pre-order' %}{{ 'collections.general.pre_order' | t }}
      {% else %}{{ 'products.product.add_to_cart' | t }}
      {% endif %}
      {% endcapture %}

Here's one version of the code I modified for two different alternates:

      {% capture add_to_cart_label %}
      {% if collection_handles contains 'special-order' %}{{ 'collections.general.special_order' | t }}{% endif %}
      {% if collection_handles contains 'pre-order' %}{{ 'collections.general.pre_order' | t }}
      {% else %}{{ 'products.product.add_to_cart' | t }}{% endif %}
      {% endcapture %};

This almost works, except that the "Pre-Order" option displays both the "Pre-Order" text and the general "Add to Cart" text. The Special Order text and regular Add to Cart text both display correctly.

Here's another version:

      {% capture add_to_cart_label %}
      {% if collection_handles contains 'special-order' %}{{ 'collections.general.special_order' | t }}
      {% else %}{{ 'products.product.add_to_cart' | t }}{% endif %}
      {% if collection_handles contains 'pre-order' %}{{ 'collections.general.pre_order' | t }}
      {% else %}{{ 'products.product.add_to_cart' | t }}{% endif %}
      {% endcapture %}

This one displays both the alternate text AND the standard add-to-cart text in the same button, and then doubles it on the regular buttons.

My personal logic processors say it should look like this:

      {% capture add_to_cart_label %}
      {% if collection_handles contains 'special-order' %}{{ 'collections.general.special_order' | t }}{% endif %}
      {% if collection_handles contains 'pre-order' %}{{ 'collections.general.pre_order' | t }}{% endif %}
      {% else %}{{ 'products.product.add_to_cart' | t }}
      {% endcapture %}

But that means that the {% else %} tag is outside of the {% if %} tag, so it gives me a syntax error, and I've discovered through trial and error that there's no "or" tag that I can sub for the second "if" conditional (for "pre-order").

Help! I'm happy to provide screen shots if that helps.

Replies 3 (3)
Jason
Shopify Expert
10438 167 2081

Use elsif.

{% if foo == bar %}
  ...
{% elsif a == b %}
  ...
{% else %}
  ...
{% endif %}

 

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.
Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.
★ http://freakdesign.com.au ★
Captain_s_Staff
New Member
4 0 0

That worked perfectly -- thank you so much!!

Giants_Fan
New Member
21 0 0

I'm facing a similar issue.

I have customizable products (signs, stickers) where customers can either design online or upload their artwork using a 3rd party app, or they can email us their artwork.  The app places 2 buttons on the product page, a "customize" button and an "upload file" button.  The shopify add to cart button is also present. 

I'd like to have the "add to cart" button read, "Email artwork later" if the customer hasn't either uploaded a file or submitted a design through the design online button.  And once the customer either uploads their artwork, or submits a design through the app, then the "email artwork later" button changes to read "add to cart".  The functionality of the button will stay the same either way it reads. it will still be the add to cart button. 

And once I have that figured out, I'd like to setup my email confirmations so if they click the add to cart button when it reads, "email artwork later" they get an email confirmation asking for their artwork, if they click it when it reads "add to cart" they get an email confirmation explaining that we will send them a proof within 24 hours.

I'm using supply theme, here is my url, https://nationalsignsource.com/collections/customizable-signs/products/rectangle-stickers-customize-...