Skip to content

Instantly share code, notes, and snippets.

@gterrill
Created June 4, 2015 02:55
Show Gist options
  • Save gterrill/2fdf2ce3d2037de8d429 to your computer and use it in GitHub Desktop.
Save gterrill/2fdf2ce3d2037de8d429 to your computer and use it in GitHub Desktop.
Disable add to cart on product page if product already in the cart
{% comment %}
BookThatApp auto installed snippet. Changes made to this file will be lost if installed again.
{% endcomment %}
{% if product.metafields.bookthatapp.config %}
{% unless bta_configured %}
<div class="booking-form">
<p id="bta-loading" class="loading">Loading availability...</p>
<div id="variant-in-cart" style="display:none">
<p>Item already in cart.</p>
<a href="#" onclick="removeBookingItem(); return false;">Remove</a> to change dates.
<a id="to-cart" href="/cart" class="btn">Go to cart</a>
</div>
<div class="hide-if-already-in-cart" style="display:none">
<div class="selector-wrapper">
{% capture attribute %}booking-start{% endcapture %}
<label for="{{ attribute }}-{{ product.handle }}">Select a Date:</label>
<input id="{{ attribute }}-{{ product.handle }}" type="text" name="properties[{{ attribute }}]" size="12" class="datepicker bta required bta-load-enable" data-handle="{{ product.handle }}" data-variant="{{ product.variants.first.id}}" data-bta-product-config="{{ product.metafields.bookthatapp.config }}" data-bta-variant-config="{% for variant in product.variants %}{{ variant.id }}:{{ variant.metafields.bookthatapp.config }}{% unless forloop.last %},{% endunless %}{% endfor %}" disabled="disabled" />
<div class="bta-scheduled-times-widget" data-label="Select a Time:" data-prompt="Please choose a date..." data-datepicker="{{ attribute }}">
<p class="loading">Loading class schedule...</p>
<div class="booked-out-message" style="display:none">Sorry, that time is not available.</div>
</div>
</div>
</div>
</div>
<script>
var bta = {
productId: {{ product.id }},
cart: {{ cart | json }},
callbacks: {
available: function() {
$('#bta-loading').hide();
checkCart();
}
}
}
$('form[action="/cart/add"]').on('change', '.single-option-selector', function() {
if ($('#{{ attribute }}-{{ product.handle }}').hasClass('bta-initialized')) {
checkCart();
}
});
function checkCart() {
var variant = parseInt(bta.findSelectedVariantId(), 10),
carted = false;
for (var i = 0; i < bta.cart.items.length; i++) {
if (bta.cart.items[i].product_id == {{ product.id}}) {
carted = true;
break;
}
}
if (carted) {
$('#variant-in-cart').show();
$('.hide-if-already-in-cart').hide();
} else {
$('#variant-in-cart').hide();
$('.hide-if-already-in-cart').show();
}
}
function removeBookingItem() {
Shopify.removeItem(bta.findSelectedVariantId(), function() {
window.location.href = window.location.pathname;
})
}
</script>
{% assign bta_configured = true %}
{% endunless %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment