Skip to content
This repository has been archived by the owner on Apr 26, 2023. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/1.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Dominiak committed May 30, 2019
2 parents 39a2d51 + 0eab00d commit 4ddde8d
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Snowdog_SolrNavigation/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
};
18 changes: 12 additions & 6 deletions Snowdog_SolrNavigation/templates/grid.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $solrDataHelper = $this->helper('Snowdog\SolrNavigation\Helper\Data');
<div class="badge catalog-grid-item__badge">
<?= __('NEW') ?>
</div>
<?php endif; ?>
<?php endif ?>
<a
href="<?= $block->getProductUrl($_product) ?>"
class="catalog-grid-item__link"
Expand Down Expand Up @@ -80,10 +80,16 @@ $solrDataHelper = $this->helper('Snowdog\SolrNavigation\Helper\Data');
<div class="catalog-grid-item__actions">
<form
class="catalog-grid-item__primary-form"
action="<?= $block->escapeHtml($block->getAddToUrl('cart', $_product['entity_id'])) ?>"
action="<?= $block->escapeUrl($block->getAddToUrl('cart', $_product['entity_id'])) ?>"
method="post"
data-mage-init='{ "validation": {} }'
data-mage-init='{ "solrAddToCart": {} }'
>
<input
type="hidden"
name="form_key"
value=""
>

<button
class="
button
Expand Down Expand Up @@ -137,7 +143,7 @@ $solrDataHelper = $this->helper('Snowdog\SolrNavigation\Helper\Data');
</div>
</section>
</li>
<?php endforeach; ?>
<?php endforeach ?>
</ul>
<?php endif; ?>
<?php endif; ?>
<?php endif ?>
<?php endif ?>
20 changes: 14 additions & 6 deletions Snowdog_SolrNavigation/templates/list.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ $solrDataHelper = $this->helper('Snowdog\SolrNavigation\Helper\Data');
<div class="catalog-list-item__actions">
<div class="catalog-list-item__actions-primary">
<form
action="<?= $block->escapeHtml($block->getAddToUrl('cart', $_product['entity_id'])) ?>"
action="<?= $block->escapeUrl($block->getAddToUrl('cart', $_product['entity_id'])) ?>"
method="post"
data-mage-init='{ "validation": {} }'
data-mage-init='{ "solrAddToCart": {} }'
>
<input
type="hidden"
name="form_key"
value=""
>

<button
class="
button
Expand All @@ -86,7 +92,9 @@ $solrDataHelper = $this->helper('Snowdog\SolrNavigation\Helper\Data');
"
type="submit"
>
<?= __('Add to Cart') ?>
<span>
<?= __('Add to Cart') ?>
</span>
</button>
</form>
</div>
Expand Down Expand Up @@ -127,6 +135,6 @@ $solrDataHelper = $this->helper('Snowdog\SolrNavigation\Helper\Data');
</div>
</div>
</section>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>
<?php endforeach ?>
<?php endif ?>
<?php endif ?>
4 changes: 2 additions & 2 deletions Snowdog_SolrNavigation/templates/navigation.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $facets = $this->getFacets();
<ul class="dropdown-list__list">
<?php foreach ($facets as $attributeCode => $facetBlock): ?>
<?= $facetBlock->toHtml() ?>
<?php endforeach; ?>
<?php endforeach ?>
</ul>
</div>
<?php endif; ?>
<?php endif ?>
143 changes: 143 additions & 0 deletions Snowdog_SolrNavigation/web/js/solr-add-to-cart.js
Original file line number Diff line number Diff line change
@@ -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;
});

0 comments on commit 4ddde8d

Please sign in to comment.