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
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.
Similar questions
Find the answer in similar questions on our website.
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.