Silver Hampshire
Silver Hampshire

Reputation: 27

Shopify disable 'add to cart' button if variant is in cart or if there are already 5 in the cart

I am working on Shopify. I have 2 'add to cart' buttons set up on the product pages, the first one for the 1st variant of metres, and a second button for the last variant of sample. This is the same on all products. A customer can order as many metres as they would like, but we want the customer to be limited to only 1 sample of each product/fabric and only 5 samples (of any product/fabric) in total per order.

I have done some research and it would seem I need to use callback but I don't know how to write/create script of any kind so not sure how to do this.

Here is my coding for the second button:

<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 }}" />

Here is the coding for the cart:

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

And then finally here is the script with regards to the button

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

Thank you for any help - if I am missing anything or you would like a better explanation then please let me know (and apologies for not being clear enough).

Just to reiterate - the sample is always the last variant out of 2 for each product. This functionality of disabling if already in cart and max of 5 samples (no matter what products) is only for the sample variant.

Many thanks

Upvotes: 1

Views: 3361

Answers (1)

MyShopify Consultants
MyShopify Consultants

Reputation: 136

You need to take a look at this

https://cartjs.org/

You can do it without but its going to be a lot harder, you'll need some experience though, you might struggle without.

Upvotes: 2

Related Questions