【问题标题】:Shopify disable 'add to cart' button if variant is in cart or if there are already 5 in the cart如果购物车中有变体或购物车中已有 5 个,Shopify 禁用“添加到购物车”按钮
【发布时间】:2016-11-03 11:08:27
【问题描述】:

我正在开发 Shopify。我在产品页面上设置了 2 个“添加到购物车”按钮,第一个按钮用于米的第一个变体,第二个按钮用于样品的最后一个变体。这在所有产品上都是一样的。客户可以订购任意数量的仪表,但我们希望客户在每个订单中仅限于每种产品/面料的 1 个样品,并且(任何产品/面料的)总共只有 5 个样品。

我做了一些研究,似乎我需要使用回调,但我不知道如何编写/创建任何类型的脚本,所以不知道该怎么做。

这是我对第二个按钮的编码:

<section class="sample-area">
    <hr style="width: 50%; margin: 20px 25%;" />
    <h3 style="text-transform: uppercase; padding-bottom: 15px;">Order a free sample</h3>
<form class="sample-form" id="sample-form" method="post" action="/cart/add">
<input type="hidden" id="quantity" name="quantity" value="1">
<input type="hidden" name="id" value="{{ product.variants.last.id }}" data-variant-title="{{ product.variants.last.title }}"/>
<input type="submit" class="action-button enter" value="{{ 'products.product.add_to_cart' | t }}" />

这是购物车的编码:

<td class="price">{% if item.price > 0 %}<span class="money mets">{{ item.price | money }}</span>{% else %}<span>FREE</span>{% endif %}</td>
    <td class="quantity">{% if item.variant.title contains "Sample" %}<input type="hidden" class="field" value="{{ item.quantity }}" data-id="{{ item.variant.id }}">{{ item.quantity }}
      {% else %}
      <select id="updates_{{ item.variant.id }}" name="updates[]" class="drops"> 
    {% for i in (2..20) %}
    <option value="{{ i }}" {% if item.quantity == i %} selected{% endif %}>{{ i }}</option>
    {% endfor %}
    </select>{% endif %}</td>
    <td class="total"><span class="money">{{ item.quantity | times: item.price | money }}</span></td>
    <td class="remove last"><a href="/cart/change/{{ item.variant.id }}?line={{ forloop.index }}&quantity=0">v</a></td>

最后是关于按钮的脚本

    ProductView.prototype.events = {
      "click #product-area .thumb": "determineSelectedThumb",
      "click .fullscreen-product-viewer .thumb": "determineSelectedThumb",
      "click .toggle-fullview": "openFullview",
      "click .fullscreen-product-viewer": "closeFullview",
      "click .fullscreen-product-viewer .modal": "stopProp",
      "click #product-area .submit": "addProductToCart",
      "click #product-area .enter": "addSampleToCart",
      "click .modal-wrap .close": "closeFullview",
      "change #product-area .single-option-selector": "resetErrors"
    };

        ProductView.prototype.addSampleToCart = function(e) {
      var quantity, submitButton, variant;
      if (Theme.productQuickAdd) {
        e.preventDefault();
        submitButton = this.$(e.target);
        if (this.processing === false) {
          submitButton.data("original-text", submitButton.val()).val(Theme.pleaseWait).addClass("disabled");
          this.processing = true;
          variant = this.$(".product.variants.last.id").val();
          quantity = this.$("1").val();
          return Shopify.addItemFromForm('sample-form', (function(_this) {
            return function(product) {
              Shopify.getCart(function(cart) {
                return _this.updateMiniCart(cart);
              });
              submitButton.val(Theme.addedToCart);
return setTimeout(function() {
                submitButton.val(submitButton.data("original-text")).removeClass("disabled");
                return _this.processing = false;
              }, 2000);
            };
          })(this));
        } else {
          return false;
        }
      }
    };

感谢您的帮助 - 如果我遗漏了什么或者您想要更好的解释,请告诉我(并为不够清楚而道歉)。

重申一下 - 样品始终是每种产品 2 个变体中的最后一个变体。如果已在购物车中且最多 5 个样品(无论是什么产品),则禁用此功能仅适用于样品变体。

非常感谢

【问题讨论】:

    标签: jquery shopify liquid


    【解决方案1】:

    你需要看看这个

    https://cartjs.org/

    没有你也可以做到,但难度会大得多,不过你需要一些经验,没有你可能会遇到困难。

    【讨论】:

    • 嗨,谢谢。不幸的是,我不熟练使用 javascript,因此并不真正了解我应该做什么。您能否给我更多指导或推荐一个我可以付钱接手工作的 javascript 编码器?谢谢
    猜你喜欢
    • 2018-09-30
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多