Last active
July 4, 2020 12:01
-
-
Save gterrill/d6e985ceeeb229c348c77949a05fb828 to your computer and use it in GitHub Desktop.
Add a booking deposit product to the cart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% comment %} | |
Adds deposit products to the cart based on how many | |
booking products are in the cart. | |
Instructions: | |
1. Create a snippet called 'booking-deposit' and paste the contents of this gist. Also create a new Deposit product (must be visible in Online channel) | |
2. Include the snippet in cart.liquid (at the bottom): | |
{% include 'booking-deposit' %} | |
3. (still in cart.liquid) Make the quantity field read only for rental and deposit products: | |
{% if item.product.metafields.bookthatapp.config or item.product.handle == 'deposit' %} | |
// show quantity amount and make the input field a hidden one | |
{% else %} | |
// normal quantity input here | |
{% endif %} | |
4. (still in cart.liquid) Don't allow removal of deposit products: | |
{% unless item.product.handle == 'deposit' %} | |
// normal remove code here | |
{% endunless %} | |
{% endcomment %} | |
{% assign deposit_id = all_products['deposit'].variants.first.id %} | |
<script> | |
Depositor = { | |
updateQuantity: function(qty) { | |
var data = { | |
updates: { | |
{{ deposit_id }}: qty | |
} | |
}; | |
jQuery.ajax({ | |
type: 'POST', | |
url: '/cart/update.js', | |
data: data, | |
dataType: 'json', | |
success: function() { document.location.href = '/cart'; }, | |
error: function(jqXHR, textStatus, errorThrown) { | |
console.log([jqXHR, textStatus, errorThrown]); | |
} | |
}); | |
} | |
} | |
{% assign deposit_count = 0 %} | |
{% assign bta_product_count = 0 %} | |
{% for item in cart.items %} | |
{% if item.id == deposit_id %} | |
{% assign deposit_count = item.quantity %} | |
{% endif %} | |
{% if item.product.metafields.bookthatapp.config %} | |
{% capture bta_product_count %}{{ bta_product_count | plus: 1 }}{% endcapture %} | |
{% endif %} | |
{% endfor %} | |
{% assign bta_product_count = bta_product_count | plus:0 %} | |
// load api.jquery.js using async | |
var s = document.createElement( 'script' ); | |
s.setAttribute('src', "{{ 'api.jquery.js' | shopify_asset_url }}"); | |
s.async = true; | |
document.body.appendChild(s); | |
document.addEventListener("DOMContentLoaded", function(event) { | |
{% if cart.items.size == 1 and deposit_count == 1 %} | |
// no products other than deposit in cart | |
Depositor.updateQuantity(0); | |
{% elsif deposit_count != bta_product_count %} | |
// products: {{ bta_product_count }} / deposits: {{ deposit_count }} | |
Depositor.updateQuantity({{ bta_product_count }}); | |
{% endif %} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I am hoping to get some extra instructions on this too please! Step 3 to 4 have stumped me...