Shopify如果声明

发布于 2025-01-31 12:24:39 字数 3205 浏览 1 评论 0原文

我需要为供应商是特定供应商的产品运行一些逻辑,并且如果产品含量不足,请禁用“添加免费示例”按钮。 (已经设置了禁用该按钮的代码),这是以下代码:

{%- 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 %}

我觉得这个问题可能位于“ Isinstock”变量的某个地方,但不确定。我需要它仅在其某些供应商指定的供应商时才禁用该按钮,并且该产品不合时间

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 %}

I have a feeling the issue may lie somewhere with the 'isInStock' variable but not sure. I need it to only disable the button if its the certain vendor is the one specified and the product is out of stock

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文