spree_frontend 3.6.6 → 3.7.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/spree/frontend.js +8 -0
  3. data/app/assets/javascripts/spree/frontend/api_tokens.js +17 -0
  4. data/app/assets/javascripts/spree/frontend/cart.js +59 -0
  5. data/app/assets/javascripts/spree/frontend/checkout.js +19 -0
  6. data/app/assets/javascripts/spree/frontend/checkout/address.js +102 -0
  7. data/app/assets/javascripts/spree/frontend/checkout/payment.js +57 -0
  8. data/app/assets/javascripts/spree/frontend/checkout/shipment.js +47 -0
  9. data/app/assets/javascripts/spree/frontend/coupon_manager.js +45 -0
  10. data/app/assets/javascripts/spree/frontend/product.js +104 -51
  11. data/app/controllers/spree/checkout_controller.rb +7 -13
  12. data/app/controllers/spree/locale_controller.rb +1 -3
  13. data/app/controllers/spree/orders_controller.rb +25 -10
  14. data/app/controllers/spree/store_controller.rb +12 -0
  15. data/app/helpers/spree/frontend_helper.rb +8 -12
  16. data/app/views/spree/checkout/_payment.html.erb +3 -3
  17. data/app/views/spree/checkout/edit.html.erb +1 -1
  18. data/app/views/spree/orders/edit.html.erb +1 -2
  19. data/app/views/spree/products/_cart_form.html.erb +10 -4
  20. data/app/views/spree/products/_properties.html.erb +1 -1
  21. data/app/views/spree/products/_thumbnails.html.erb +4 -2
  22. data/config/routes.rb +3 -0
  23. data/lib/generators/spree/frontend/copy_views/copy_views_generator.rb +1 -1
  24. data/spree_frontend.gemspec +2 -2
  25. metadata +18 -23
  26. data/app/assets/javascripts/spree/frontend/cart.js.coffee +0 -28
  27. data/app/assets/javascripts/spree/frontend/checkout.js.coffee +0 -16
  28. data/app/assets/javascripts/spree/frontend/checkout/address.js.coffee +0 -81
  29. data/app/assets/javascripts/spree/frontend/checkout/payment.js.coffee +0 -57
  30. data/app/assets/javascripts/spree/frontend/checkout/shipment.js.coffee +0 -41
  31. data/app/assets/javascripts/spree/frontend/coupon_manager.js.coffee +0 -37
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5de97052624944fa1d047c1ce80b59ce6f5b2bbc0dc1ec9638b21123d6a72c7
4
- data.tar.gz: fda1112d43eec35e71116b0dbb7c85983903914ac01779366612c3e7c6999ff3
3
+ metadata.gz: a0e2c124c039c8effa704a756c493bb5ef7f033c75e62a7d212ce6a09b8b037c
4
+ data.tar.gz: d5fa482f5b8560847b2d3453ae8e7d312c8ffd2496b9e775d628f4c8bc7cef39
5
5
  SHA512:
6
- metadata.gz: 5f0220854356664030033a5f585bbc609b782a1b015a049997ac415120a4fca0d499029d46ab62414c756300fb1617332c24a32122548720af184d8156083dc0
7
- data.tar.gz: 81195e4e2e29c62fee1b474002bb0f3c91f980adc275253ada1839c863cb7b844d04b004f3a91ea0794f6e310a31a7b04446a34d76d8514ffee799d0189fe756
6
+ metadata.gz: 31e2ddb024d889816abdd39115a47ff816cac233d541307490392f327c46b18329dda1c0c16eec0b36f6c38c04ebd675c20a15fa7346f53533867f656f4d9657
7
+ data.tar.gz: 87c016d53c0bf16c7e298bca4a4009020a37bb05fa23fe240827d00beb56f2b0b707214d23169ad3c6d467f6f884e83ac20548a885f249ddccb5e84599799ba4
@@ -1,8 +1,16 @@
1
1
  //= require bootstrap-sprockets
2
2
  //= require jquery.payment
3
3
  //= require spree
4
+ //= require polyfill.min
5
+ //= require fetch.umd
6
+ //= require spree/api/main
7
+ //= require spree/frontend/api_tokens
4
8
  //= require spree/frontend/cart
5
9
  //= require spree/frontend/checkout
6
10
  //= require spree/frontend/checkout/address
7
11
  //= require spree/frontend/checkout/payment
8
12
  //= require spree/frontend/product
13
+
14
+ Spree.routes.api_tokens = Spree.pathFor('api_tokens')
15
+ Spree.routes.ensure_cart = Spree.pathFor('ensure_cart')
16
+ Spree.routes.api_v2_storefront_cart_apply_coupon_code = Spree.pathFor('api/v2/storefront/cart/apply_coupon_code')
@@ -0,0 +1,17 @@
1
+ Spree.fetchApiTokens = function () {
2
+ fetch(Spree.routes.api_tokens, {
3
+ method: 'GET',
4
+ credentials: 'same-origin'
5
+ }).then(function (response) {
6
+ switch (response.status) {
7
+ case 200:
8
+ response.json().then(function (json) {
9
+ SpreeAPI.orderToken = json.order_token
10
+ SpreeAPI.oauthToken = json.oauth_token
11
+ })
12
+ break
13
+ }
14
+ })
15
+ }
16
+
17
+ Spree.ready(function () { Spree.fetchApiTokens() })
@@ -0,0 +1,59 @@
1
+ //= require spree/frontend/coupon_manager
2
+
3
+ Spree.ready(function ($) {
4
+ var formUpdateCart = $('form#update-cart')
5
+ if (formUpdateCart.length) {
6
+ $('form#update-cart a.delete').show().one('click', function () {
7
+ $(this).parents('.line-item').first().find('input.line_item_quantity').val(0)
8
+ $(this).parents('form').first().submit()
9
+ return false
10
+ })
11
+ }
12
+ formUpdateCart.submit(function (event) {
13
+ var input = {
14
+ couponCodeField: $('#order_coupon_code'),
15
+ couponStatus: $('#coupon_status')
16
+ }
17
+ var updateButton = $('form#update-cart #update-button')
18
+ updateButton.attr('disabled', true)
19
+ if ($.trim(input.couponCodeField.val()).length > 0) {
20
+ // eslint-disable-next-line no-undef
21
+ if (new CouponManager(input).applyCoupon()) {
22
+ this.submit()
23
+ return true
24
+ } else {
25
+ updateButton.attr('disabled', false)
26
+ event.preventDefault()
27
+ return false
28
+ }
29
+ }
30
+ })
31
+ })
32
+
33
+ Spree.fetch_cart = function () {
34
+ return $.ajax({
35
+ url: Spree.pathFor('cart_link')
36
+ }).done(function (data) {
37
+ return $('#link-to-cart').html(data)
38
+ })
39
+ }
40
+
41
+ Spree.ensureCart = function (successCallback) {
42
+ if (SpreeAPI.orderToken) {
43
+ successCallback()
44
+ } else {
45
+ fetch(Spree.routes.ensure_cart, {
46
+ method: 'POST',
47
+ credentials: 'same-origin'
48
+ }).then(function (response) {
49
+ switch (response.status) {
50
+ case 200:
51
+ response.json().then(function (json) {
52
+ SpreeAPI.orderToken = json.token
53
+ successCallback()
54
+ })
55
+ break
56
+ }
57
+ })
58
+ }
59
+ }
@@ -0,0 +1,19 @@
1
+ Spree.disableSaveOnClick = function () {
2
+ $('form.edit_order').on('submit', function (event) {
3
+ if ($(this).data('submitted') === true) {
4
+ event.preventDefault()
5
+ } else {
6
+ $(this).data('submitted', true)
7
+ $(this).find(':submit, :image').removeClass('primary').addClass('disabled')
8
+ }
9
+ })
10
+ }
11
+
12
+ Spree.enableSave = function () {
13
+ $('form.edit_order').data('submitted', false).find(':submit, :image').attr('disabled', false).addClass('primary').removeClass('disabled')
14
+ }
15
+
16
+ Spree.ready(function () {
17
+ Spree.Checkout = {}
18
+ return Spree.Checkout
19
+ })
@@ -0,0 +1,102 @@
1
+ Spree.ready(function ($) {
2
+ Spree.onAddress = function () {
3
+ if ($('#checkout_form_address').length) {
4
+ Spree.updateState = function (region) {
5
+ var countryId = getCountryId(region)
6
+ if (countryId != null) {
7
+ if (Spree.Checkout[countryId] == null) {
8
+ $.get(Spree.routes.states_search, {
9
+ country_id: countryId
10
+ }).done(function (data) {
11
+ Spree.Checkout[countryId] = {
12
+ states: data.states,
13
+ states_required: data.states_required
14
+ }
15
+ Spree.fillStates(Spree.Checkout[countryId], region)
16
+ })
17
+ } else {
18
+ Spree.fillStates(Spree.Checkout[countryId], region)
19
+ }
20
+ }
21
+ }
22
+ Spree.fillStates = function (data, region) {
23
+ var selected, statesWithBlank
24
+ var statesRequired = data.states_required
25
+ var states = data.states
26
+ var statePara = $('#' + region + 'state')
27
+ var stateSelect = statePara.find('select')
28
+ var stateInput = statePara.find('input')
29
+ var stateSpanRequired = statePara.find('[id$="state-required"]')
30
+ if (states.length > 0) {
31
+ selected = parseInt(stateSelect.val())
32
+ stateSelect.html('')
33
+ statesWithBlank = [
34
+ {
35
+ name: '',
36
+ id: ''
37
+ }
38
+ ].concat(states)
39
+ $.each(statesWithBlank, function (idx, state) {
40
+ var opt = $(document.createElement('option')).attr('value', state.id).html(state.name)
41
+ if (selected === state.id) {
42
+ opt.prop('selected', true)
43
+ }
44
+ stateSelect.append(opt)
45
+ })
46
+ stateSelect.prop('disabled', false).show()
47
+ stateInput.hide().prop('disabled', true)
48
+ statePara.show()
49
+ stateSpanRequired.show()
50
+ if (statesRequired) {
51
+ stateSelect.addClass('required')
52
+ }
53
+ stateSelect.removeClass('hidden')
54
+ stateInput.removeClass('required')
55
+ } else {
56
+ stateSelect.hide().prop('disabled', true)
57
+ stateInput.show()
58
+ if (statesRequired) {
59
+ stateSpanRequired.show()
60
+ stateInput.addClass('required')
61
+ } else {
62
+ stateInput.val('')
63
+ stateSpanRequired.hide()
64
+ stateInput.removeClass('required')
65
+ }
66
+ statePara.toggle(!!statesRequired)
67
+ stateInput.prop('disabled', !statesRequired)
68
+ stateInput.removeClass('hidden')
69
+ stateSelect.removeClass('required')
70
+ }
71
+ }
72
+ $('#bcountry select').change(function () {
73
+ Spree.updateState('b')
74
+ })
75
+ $('#scountry select').change(function () {
76
+ Spree.updateState('s')
77
+ })
78
+ Spree.updateState('b')
79
+
80
+ var orderUseBilling = $('input#order_use_billing')
81
+ orderUseBilling.change(function () {
82
+ updateShippingFormState(orderUseBilling)
83
+ })
84
+ updateShippingFormState(orderUseBilling)
85
+ }
86
+ function updateShippingFormState (orderUseBilling) {
87
+ if (orderUseBilling.is(':checked')) {
88
+ $('#shipping .inner').hide()
89
+ $('#shipping .inner input, #shipping .inner select').prop('disabled', true)
90
+ } else {
91
+ $('#shipping .inner').show()
92
+ $('#shipping .inner input, #shipping .inner select').prop('disabled', false)
93
+ Spree.updateState('s')
94
+ }
95
+ }
96
+
97
+ function getCountryId (region) {
98
+ return $('#' + region + 'country select').val()
99
+ }
100
+ }
101
+ Spree.onAddress()
102
+ })
@@ -0,0 +1,57 @@
1
+ //= require spree/frontend/coupon_manager
2
+ Spree.ready(function ($) {
3
+ Spree.onPayment = function () {
4
+ if ($('#checkout_form_payment').length) {
5
+ if ($('#existing_cards').length) {
6
+ $('#payment-method-fields').hide()
7
+ $('#payment-methods').hide()
8
+ $('#use_existing_card_yes').click(function () {
9
+ $('#payment-method-fields').hide()
10
+ $('#payment-methods').hide()
11
+ $('.existing-cc-radio').prop('disabled', false)
12
+ })
13
+ $('#use_existing_card_no').click(function () {
14
+ $('#payment-method-fields').show()
15
+ $('#payment-methods').show()
16
+ $('.existing-cc-radio').prop('disabled', true)
17
+ })
18
+ }
19
+ $('.cardNumber').payment('formatCardNumber')
20
+ $('.cardExpiry').payment('formatCardExpiry')
21
+ $('.cardCode').payment('formatCardCVC')
22
+ $('.cardNumber').change(function () {
23
+ $(this).parent().siblings('.ccType').val($.payment.cardType(this.value))
24
+ })
25
+ $('input[type="radio"][name="order[payments_attributes][][payment_method_id]"]').click(function () {
26
+ $('#payment-methods li').hide()
27
+ if (this.checked) {
28
+ $('#payment_method_' + this.value).show()
29
+ }
30
+ })
31
+ $(document).on('click', '#cvv_link', function (event) {
32
+ var windowName = 'cvv_info'
33
+ var windowOptions = 'left=20,top=20,width=500,height=500,toolbar=0,resizable=0,scrollbars=1'
34
+ window.open($(this).attr('href'), windowName, windowOptions)
35
+ event.preventDefault()
36
+ })
37
+ $('input[type="radio"]:checked').click()
38
+ $('#checkout_form_payment').submit(function (event) {
39
+ var input = {
40
+ couponCodeField: $('#order_coupon_code'),
41
+ couponStatus: $('#coupon_status')
42
+ }
43
+ if ($.trim(input.couponCodeField.val()).length > 0) {
44
+ // eslint-disable-next-line no-undef
45
+ if (new CouponManager(input).applyCoupon()) {
46
+ return true
47
+ } else {
48
+ Spree.enableSave()
49
+ event.preventDefault()
50
+ return false
51
+ }
52
+ }
53
+ })
54
+ }
55
+ }
56
+ Spree.onPayment()
57
+ })
@@ -0,0 +1,47 @@
1
+ /* global accounting */
2
+ function ShippingTotalManager (input1) {
3
+ this.input = input1
4
+ this.shippingMethods = this.input.shippingMethods
5
+ this.shipmentTotal = this.input.shipmentTotal
6
+ this.orderTotal = this.input.orderTotal
7
+ this.formatOptions = {
8
+ symbol: this.shipmentTotal.data('currency'),
9
+ decimal: this.shipmentTotal.attr('decimal-mark'),
10
+ thousand: this.shipmentTotal.attr('thousands-separator'),
11
+ precision: 2
12
+ }
13
+ }
14
+
15
+ ShippingTotalManager.prototype.calculateShipmentTotal = function () {
16
+ var checked = $(this.shippingMethods).filter(':checked')
17
+ this.sum = 0
18
+ $.each(checked, function (idx, shippingMethod) {
19
+ this.sum += this.parseCurrencyToFloat($(shippingMethod).data('cost'))
20
+ }.bind(this))
21
+ return this.readjustSummarySection(this.parseCurrencyToFloat(this.orderTotal.html()), this.sum, this.parseCurrencyToFloat(this.shipmentTotal.html()))
22
+ }
23
+
24
+ ShippingTotalManager.prototype.parseCurrencyToFloat = function (input) {
25
+ return accounting.unformat(input, this.formatOptions.decimal)
26
+ }
27
+
28
+ ShippingTotalManager.prototype.readjustSummarySection = function (orderTotal, newShipmentTotal, oldShipmentTotal) {
29
+ var newOrderTotal = orderTotal + (newShipmentTotal - oldShipmentTotal)
30
+ this.shipmentTotal.html(accounting.formatMoney(newShipmentTotal, this.formatOptions))
31
+ return this.orderTotal.html(accounting.formatMoney(newOrderTotal, this.formatOptions))
32
+ }
33
+
34
+ ShippingTotalManager.prototype.bindEvent = function () {
35
+ this.shippingMethods.change(function () {
36
+ return this.calculateShipmentTotal()
37
+ }.bind(this))
38
+ }
39
+
40
+ Spree.ready(function ($) {
41
+ var input = {
42
+ orderTotal: $('#summary-order-total'),
43
+ shipmentTotal: $('[data-hook="shipping-total"]'),
44
+ shippingMethods: $('input[data-behavior="shipping-method-selector"]')
45
+ }
46
+ return new ShippingTotalManager(input).bindEvent()
47
+ })
@@ -0,0 +1,45 @@
1
+ function CouponManager (input) {
2
+ this.input = input
3
+ this.couponCodeField = this.input.couponCodeField
4
+ this.couponApplied = false
5
+ this.couponStatus = this.input.couponStatus
6
+ }
7
+
8
+ CouponManager.prototype.applyCoupon = function () {
9
+ this.couponCode = $.trim($(this.couponCodeField).val())
10
+ if (this.couponCode !== '') {
11
+ if (this.couponStatus.length === 0) {
12
+ this.couponStatus = $('<div/>', {
13
+ id: 'coupon_status'
14
+ })
15
+ this.couponCodeField.parent().append(this.couponStatus)
16
+ }
17
+ this.couponStatus.removeClass()
18
+ this.sendRequest()
19
+ return this.couponApplied
20
+ } else {
21
+ return true
22
+ }
23
+ }
24
+
25
+ CouponManager.prototype.sendRequest = function () {
26
+ return $.ajax({
27
+ async: false,
28
+ method: 'PATCH',
29
+ url: Spree.routes.api_v2_storefront_cart_apply_coupon_code,
30
+ dataType: 'json',
31
+ headers: {
32
+ 'X-Spree-Order-Token': Spree.current_order_token
33
+ },
34
+ data: {
35
+ coupon_code: this.couponCode
36
+ }
37
+ }).done(function () {
38
+ this.couponCodeField.val('')
39
+ this.couponStatus.addClass('alert-success').html(Spree.translations.coupon_code_applied)
40
+ this.couponApplied = true
41
+ }.bind(this)).fail(function (xhr) {
42
+ var handler = xhr.responseJSON
43
+ this.couponStatus.addClass('alert-error').html(handler['error'])
44
+ }.bind(this))
45
+ }
@@ -1,75 +1,128 @@
1
- Spree.ready(function ($) {
1
+ //= require spree/api/storefront/cart
2
+ //= require spree/frontend/cart
2
3
 
4
+ Spree.ready(function ($) {
3
5
  Spree.addImageHandlers = function () {
4
- var thumbnails = $("#product-images ul.thumbnails");
5
- ($("#main-image")).data("selectedThumb", ($("#main-image img")).attr("src"));
6
- thumbnails.find("li").eq(0).addClass("selected");
7
-
8
- thumbnails.find("a").on("click", function (event) {
9
- ($("#main-image")).data("selectedThumb", ($(event.currentTarget)).attr("href"));
10
- ($("#main-image")).data("selectedThumbId", ($(event.currentTarget)).parent().attr("id"));
11
- thumbnails.find("li").removeClass("selected");
12
- ($(event.currentTarget)).parent("li").addClass("selected");
13
- return false;
14
- });
15
-
16
- thumbnails.find("li").on("mouseenter", function (event) {
17
- return ($("#main-image img")).attr("src", ($(event.currentTarget)).find("a").attr("href"));
18
- });
19
-
20
- return thumbnails.find("li").on("mouseleave", function (event) {
21
- return ($("#main-image img")).attr("src", ($("#main-image")).data("selectedThumb"));
22
- });
23
- };
6
+ var thumbnails = $('#product-images ul.thumbnails');
7
+ ($('#main-image')).data('selectedThumb', ($('#main-image img')).attr('src'));
8
+ ($('#main-image')).data('selectedThumbAlt', ($('#main-image img')).attr('alt'))
9
+ thumbnails.find('li').eq(0).addClass('selected')
10
+
11
+ thumbnails.find('a').on('click', function (event) {
12
+ ($('#main-image')).data('selectedThumb', ($(event.currentTarget)).attr('href'));
13
+ ($('#main-image')).data('selectedThumbId', ($(event.currentTarget)).parent().attr('id'));
14
+ ($('#main-image')).data('selectedThumbAlt', ($(event.currentTarget)).find('img').attr('alt'))
15
+ thumbnails.find('li').removeClass('selected');
16
+ ($(event.currentTarget)).parent('li').addClass('selected')
17
+ return false
18
+ })
19
+
20
+ thumbnails.find('li').on('mouseenter', function (event) {
21
+ return ($('#main-image img'))
22
+ .attr({ 'src': ($(event.currentTarget)).find('a').attr('href'), 'alt': ($(event.currentTarget)).find('img').attr('alt') })
23
+ })
24
+
25
+ return thumbnails.find('li').on('mouseleave', function (event) {
26
+ return ($('#main-image img'))
27
+ .attr({ 'src': ($('#main-image')).data('selectedThumb'), 'alt': ($('#main-image')).data('selectedThumbAlt') })
28
+ })
29
+ }
24
30
 
25
31
  Spree.showVariantImages = function (variantId) {
26
- ($("li.vtmb")).hide();
27
- ($("li.tmb-" + variantId)).show();
28
- var currentThumb = $("#" + ($("#main-image")).data("selectedThumbId"));
32
+ ($('li.vtmb')).hide();
33
+ ($('li.tmb-' + variantId)).show()
34
+ var currentThumb = $('#' + ($('#main-image')).data('selectedThumbId'))
29
35
 
30
- if (!currentThumb.hasClass("vtmb + variantId")) {
31
- var thumb = $(($("#product-images ul.thumbnails li:visible.vtmb")).eq(0));
36
+ if (!currentThumb.hasClass('vtmb + variantId')) {
37
+ var thumb = $(($('#product-images ul.thumbnails li:visible.vtmb')).eq(0))
32
38
 
33
39
  if (!(thumb.length > 0)) {
34
- thumb = $(($("#product-images ul.thumbnails li:visible")).eq(0));
40
+ thumb = $(($('#product-images ul.thumbnails li:visible')).eq(0))
35
41
  }
36
42
 
37
- var newImg = thumb.find("a").attr("href");
38
- ($("#product-images ul.thumbnails li")).removeClass("selected");
39
- thumb.addClass("selected");
40
- ($("#main-image img")).attr("src", newImg);
41
- ($("#main-image")).data("selectedThumb", newImg);
42
- return ($("#main-image")).data("selectedThumbId", thumb.attr("id"));
43
+ var newImg = thumb.find('a').attr('href')
44
+
45
+ var newAlt = thumb.find('img').attr('alt');
46
+ ($('#product-images ul.thumbnails li')).removeClass('selected')
47
+ thumb.addClass('selected');
48
+ ($('#main-image img')).attr({ 'src': newImg, 'alt': newAlt });
49
+ ($('#main-image')).data({ 'selectedThumb': newImg, 'selectedThumbAlt': newAlt })
50
+ return ($('#main-image')).data('selectedThumbId', thumb.attr('id'))
43
51
  }
44
- };
52
+ }
45
53
 
46
54
  Spree.updateVariantPrice = function (variant) {
47
- var variantPrice = variant.data("price");
55
+ var variantPrice = variant.data('price')
48
56
 
49
57
  if (variantPrice) {
50
- return ($(".price.selling")).text(variantPrice);
58
+ return ($('.price.selling')).text(variantPrice)
51
59
  }
52
- };
60
+ }
53
61
 
54
62
  Spree.disableCartForm = function (variant) {
55
- var inStock = variant.data("in-stock");
56
- return $("#add-to-cart-button").attr("disabled", !inStock);
57
- };
63
+ var inStock = variant.data('in-stock')
64
+ return $('#add-to-cart-button').attr('disabled', !inStock)
65
+ }
58
66
 
59
- var radios = $("#product-variants input[type='radio']");
67
+ var radios = $("#product-variants input[type='radio']")
60
68
 
61
69
  if (radios.length > 0) {
62
- var selectedRadio = $("#product-variants input[type='radio'][checked='checked']");
63
- Spree.showVariantImages(selectedRadio.attr("value"));
64
- Spree.updateVariantPrice(selectedRadio);
65
- Spree.disableCartForm(selectedRadio);
70
+ var selectedRadio = $("#product-variants input[type='radio'][checked='checked']")
71
+ Spree.showVariantImages(selectedRadio.attr('value'))
72
+ Spree.updateVariantPrice(selectedRadio)
73
+ Spree.disableCartForm(selectedRadio)
66
74
 
67
75
  radios.click(function (event) {
68
- Spree.showVariantImages(this.value);
69
- Spree.updateVariantPrice($(this));
70
- return Spree.disableCartForm($(this));
71
- });
76
+ Spree.showVariantImages(this.value)
77
+ Spree.updateVariantPrice($(this))
78
+ return Spree.disableCartForm($(this))
79
+ })
72
80
  }
73
81
 
74
- return Spree.addImageHandlers();
75
- });
82
+ return Spree.addImageHandlers()
83
+ })
84
+
85
+ Spree.ready(function () {
86
+ var addToCartForm = document.getElementById('add-to-cart-form')
87
+ var addToCartButton = document.getElementById('add-to-cart-button')
88
+
89
+ if (addToCartForm) {
90
+ // enable add to cart button
91
+ if (addToCartButton) {
92
+ addToCartButton.removeAttribute('disabled')
93
+ }
94
+
95
+ addToCartForm.addEventListener('submit', function (event) {
96
+ event.preventDefault()
97
+
98
+ // prevent multiple clicks
99
+ if (addToCartButton) {
100
+ addToCartButton.setAttribute('disabled', 'disabled')
101
+ }
102
+
103
+ var variantId = addToCartForm.elements.namedItem('variant_id').value
104
+ var quantity = parseInt(addToCartForm.elements.namedItem('quantity').value, 10)
105
+
106
+ // we need to ensure that we have an existing cart we want to add the item to
107
+ // if we have already a cart assigned to this guest / user this won't create
108
+ // another one
109
+ Spree.ensureCart(
110
+ function () {
111
+ SpreeAPI.Storefront.addToCart(
112
+ variantId,
113
+ quantity,
114
+ {}, // options hash - you can pass additional parameters here, your backend
115
+ // needs to be aware of those, see API docs:
116
+ // https://github.com/spree/spree/blob/master/api/docs/v2/storefront/index.yaml#L42
117
+ function () {
118
+ // redirect with `variant_id` is crucial for analytics tracking
119
+ // provided by `spree_analytics_trackers` extension
120
+ window.location = Spree.routes.cart + '?variant_id=' + variantId.toString()
121
+ },
122
+ function (error) { alert(error) } // failure callback for 422 and 50x errors
123
+ )
124
+ }
125
+ )
126
+ })
127
+ }
128
+ })