e1commerce.com
Ask a question
  • New questions
  • With the answers
  • Unanswered
  1. Home
  2. html - Shopify if statement

527 votes
0 answers

html - Shopify if statement

I need to run some logic for products where the vendor is a specific vendor and if the product is out of stock, disable the 'Add Free Sample' button. (the code for disabling the button is already set,) Here is the code below:

{%- comment -%}SAMPLE LOGIC{%- endcomment -%}
{% if has_free_sample %}
  {%- comment -%} check if there is > 4 samples or an existing sample of the same in the cart {%- endcomment -%}
  {% assign maxSamples = false %}
  {% assign sampleInCart = false %}
  {% assign sampleCount = 0 %}
  {% for item in cart.items %}
    {% if size_option == item.variant_id %}
      {% assign sampleInCart = true %}
      {% break %}
    {% endif %}

    {%- comment -%}check if its a sample{%- endcomment -%}
    {% assign isASample = false %}
    {% for option in item.options_with_values %}
      {% if option.value contains 'Sample' %}
        {% assign isASample = true %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if isASample %}
      {% assign sampleCount = sampleCount | plus: 1 %}
      {% if sampleCount == 4 %}
        {% assign maxSamples = true %}
        {% break %}
      {% endif %}
    {% endif %}
  {% endfor %}

  {% assign isInStock = true %}
    {% if product.vendor == 'PS-Stock' and product.available == false %}
      {% assign isInStock = false %}
    {% endif %}


  {%- comment -%}ADD SAMPLE {%- endcomment -%}
  {% if maxSamples == false and sampleInCart == false and isInStock == true %}
    <input type="hidden" data-free-sample-quantity-input max="1"/>
    <button {% unless current_variant.available %}disabled{% endunless %} 
    data-product-form-submit
    data-in-stock-text="{{ 'products.product.add_to_cart' | t: unit: order_measurement }}"
    data-unavailable-text="{{ 'products.product.unavailable' | t }}"
    data-oos-text="{{ 'products.product.sold_out' | t }}"
    data-success-text="{{ 'products.product.added_to_cart' | t }}"
    data-add-to-cart
    data-atc-quantity-input="[data-free-sample-quantity-input]"
    data-atc-product-id="{{ size_option }}"
    data-is-sample="true"
    data-max-samples="{{ 'products.product.max_samples' | t }}"
    data-sample-added-text="{{ 'products.product.sample_added' | t }}"
    data-sample-added-error-toast-text="{{ 'products.product.sample_added_error_toast' | t }}"
    type="submit"
    class="btn--atc btn btn-primary">{{ 'products.product.add_to_cart_free_sample' | t }}</button>
  {% else %}
    <button 
      disabled
      data-max-samples="{{ 'products.product.max_samples' | t }}"
      data-in-stock-text="{{ 'products.product.add_to_cart' | t: unit: order_measurement }}"
      data-unavailable-text="{{ 'products.product.unavailable' | t }}"
      data-oos-text="{{ 'products.product.sold_out' | t }}"
      data-success-text="{{ 'products.product.added_to_cart' | t }}"
      data-atc-product-id="{{ size_option }}"
      data-is-sample="true"
      type="submit"
      class="btn--atc btn btn-primary">
      {% if maxSamples %}
        {{ 'products.product.max_samples' | t }}
      {% else %}
        {{ 'products.product.sample_added' | t }}
      {% endif %}
    </button>
  {% endif %}
{% endif %}

Undefined asked
2022-08-16


Source

Didn't find the answer?

Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Similar questions

Find the answer in similar questions on our website.

984 @shopify/shopify-api nodejs - Set permanent access token for private app installed on only one store
758 php - Shopify shows CSP error when I try to redirect my app within the iframe
608 liquid - Shopify section schema: different content for each present block
622 html - remove bold from headings on all product pages (shopify)
306 How to update product price with graphql on shopify if there is no product variant
663 Triggering Zapier on new Shopify order containing a specific product
462 Concatenate string with html in liquid Shopify
740 Shopify Graphql how to get available products for specific collection based on location
476 css selectors - Custom font for specific language shopify
875 liquid - Splitting Shopify Multi Single Line Metafield to HTML List

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.







Latest questions:

915 Get product description with metafield in shopify

643 html - CSS animation looks wrong/broken in some places on iOS devices only?

961 Shopify refund amount wrong when refund in pending state (Admin REST API)

904 How to Add Fonts to the Shopify Admin

293 css selectors - Custom font for specific language shopify



© 2021-2023

E-mail: infot@e1commerce.com