diff --git a/CHANGELOG.md b/CHANGELOG.md index cb54aba78..d8251390f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +## [1.3.1] - 2019-05-30 +### Fixed +- Add-to-cart for solr templates with updated form_key + ## [1.3.0] - 2019-04-17 ### Changed - Wishlist and catalog elements for focus state diff --git a/Snowdog_SolrNavigation/requirejs-config.js b/Snowdog_SolrNavigation/requirejs-config.js index f2ae3e932..eafb7cc06 100644 --- a/Snowdog_SolrNavigation/requirejs-config.js +++ b/Snowdog_SolrNavigation/requirejs-config.js @@ -4,7 +4,8 @@ var config = { 'priceRange': 'Snowdog_SolrNavigation/js/price-range.babel', 'quickSearch': 'Snowdog_SolrNavigation/js/quicksearch.babel', 'range': 'Snowdog_SolrNavigation/js/range.babel', - 'toolbar': 'Snowdog_SolrNavigation/js/toolbar.babel' + 'toolbar': 'Snowdog_SolrNavigation/js/toolbar.babel', + 'solrAddToCart': 'Snowdog_SolrNavigation/js/solr-add-to-cart' } } }; diff --git a/Snowdog_SolrNavigation/templates/grid.phtml b/Snowdog_SolrNavigation/templates/grid.phtml index d98a89412..e43836cb1 100644 --- a/Snowdog_SolrNavigation/templates/grid.phtml +++ b/Snowdog_SolrNavigation/templates/grid.phtml @@ -22,7 +22,7 @@ $solrDataHelper = $this->helper('Snowdog\SolrNavigation\Helper\Data');
- + helper('Snowdog\SolrNavigation\Helper\Data');
+ +
@@ -127,6 +135,6 @@ $solrDataHelper = $this->helper('Snowdog\SolrNavigation\Helper\Data'); - - - + + + diff --git a/Snowdog_SolrNavigation/templates/navigation.phtml b/Snowdog_SolrNavigation/templates/navigation.phtml index e6454856a..d406c14f6 100755 --- a/Snowdog_SolrNavigation/templates/navigation.phtml +++ b/Snowdog_SolrNavigation/templates/navigation.phtml @@ -32,7 +32,7 @@ $facets = $this->getFacets(); - + diff --git a/Snowdog_SolrNavigation/web/js/solr-add-to-cart.js b/Snowdog_SolrNavigation/web/js/solr-add-to-cart.js new file mode 100644 index 000000000..e0c14eac4 --- /dev/null +++ b/Snowdog_SolrNavigation/web/js/solr-add-to-cart.js @@ -0,0 +1,143 @@ +define([ + 'jquery', + 'mage/translate' +], function ($, $t) { + 'use strict'; + + $.widget('snowdog.solrAddToCart', { + options: { + processStart: null, + processStop: null, + bindSubmit: true, + minicartSelector: '[data-block="minicart"]', + messagesSelector: '[data-placeholder="messages"]', + productStatusSelector: '.stock.available' + }, + + _create: function () { + if (this.options.bindSubmit) { + this._bindSubmit(); + } + }, + + _bindSubmit: function () { + var self = this; + + this.element.on('submit', function (e) { + e.preventDefault(); + self.submitForm($(this)); + }); + }, + + isLoaderEnabled: function () { + return this.options.processStart && this.options.processStop; + }, + + submitForm: function (form) { + var self = this, + cookieFormKey = $.cookie('form_key'), + action = form.attr('action'), + currentFormKey = form.find('[name="form_key"]').val(); + + if (cookieFormKey !== '' && cookieFormKey !== null) { + form.find('[name="form_key"]').val(cookieFormKey); + action = action.replace(currentFormKey, cookieFormKey); + } + + $(self.options.minicartSelector).trigger('contentLoading'); + self.disableAddToCartButton(form); + + $.ajax({ + url: action, + data: form.serialize(), + type: 'post', + dataType: 'json', + + beforeSend: function () { + if (self.isLoaderEnabled()) { + $('body').trigger(self.options.processStart); + } + }, + + success: function (res) { + var eventData, + parameters; + + $(document).trigger('ajax:addToCart', { + 'form': form, + 'response': res + }); + + if (self.isLoaderEnabled()) { + $('body').trigger(self.options.processStop); + } + + if (res.backUrl) { + eventData = { + 'form': form, + 'redirectParameters': [] + }; + // trigger global event, so other modules will be able add parameters to redirect url + $('body').trigger('catalogCategoryAddToCartRedirect', eventData); + + if (eventData.redirectParameters.length > 0) { + parameters = res.backUrl.split('#'); + parameters.push(eventData.redirectParameters.join('&')); + res.backUrl = parameters.join('#'); + } + window.location = res.backUrl; + + return; + } + + if (res.messages) { + $(self.options.messagesSelector).html(res.messages); + } + + if (res.minicart) { + $(self.options.minicartSelector).replaceWith(res.minicart); + $(self.options.minicartSelector).trigger('contentUpdated'); + } + + if (res.product && res.product.statusText) { + $(self.options.productStatusSelector) + .removeClass('available') + .addClass('unavailable') + .find('span') + .html(res.product.statusText); + } + self.enableAddToCartButton(form); + } + }); + }, + + disableAddToCartButton: function (form) { + var addToCartButtonTextWhileAdding = $t('Adding...'), + addToCartButton = $(form).find('button[type=submit]'); + + addToCartButton.prop('disabled', true); + addToCartButton.addClass('disabled'); + addToCartButton.find('span').text(addToCartButtonTextWhileAdding); + addToCartButton.attr('title', addToCartButtonTextWhileAdding); + }, + + enableAddToCartButton: function (form) { + var addToCartButtonTextAdded = $t('Added'), + addToCartButton = $(form).find('button[type=submit]'); + + addToCartButton.find('span').text(addToCartButtonTextAdded); + addToCartButton.attr('title', addToCartButtonTextAdded); + + setTimeout(function () { + var addToCartButtonTextDefault = $t('Add to Cart'); + + addToCartButton.prop('disabled', false); + addToCartButton.removeClass('disabled'); + addToCartButton.find('span').text(addToCartButtonTextDefault); + addToCartButton.attr('title', addToCartButtonTextDefault); + }, 2500); + } + }); + + return $.snowdog.solrAddToCart; +});