@@ -212,16 +218,16 @@ switch ($type = $block->getType()) {
-
+
-
+
- isSaleable()) :?>
- getTypeInstance()->hasRequiredOptions($_item)) :?>
+ isSaleable()):?>
+ getTypeInstance()->hasRequiredOptions($_item)):?>
-
+
helper(Magento\Framework\Data\Helper\PostHelper::class);
$postData = $postDataHelper->getPostData($block->escapeUrl($block->getAddToCartUrl($_item)), ['product' => $_item->getEntityId()])
?>
@@ -231,19 +237,19 @@ switch ($type = $block->getType()) {
= $block->escapeHtml(__('Add to Cart')) ?>
-
- getIsSalable()) :?>
+
+ getIsSalable()):?>
= $block->escapeHtml(__('In stock')) ?>
-
+
= $block->escapeHtml(__('Out of stock')) ?>
-
+
- getChildBlock('addto')) :?>
+ getChildBlock('addto')):?>
= $addToBlock->setProduct($_item)->getChildHtml() ?>
diff -Nuar a/vendor/magento/module-catalog/view/frontend/web/js/related-products.js b/vendor/magento/module-catalog/view/frontend/web/js/related-products.js
index c875dd8f5d2..bc54fdeac5c 100644
--- a/vendor/magento/module-catalog/view/frontend/web/js/related-products.js
+++ b/vendor/magento/module-catalog/view/frontend/web/js/related-products.js
@@ -28,10 +28,14 @@ define([
_create: function () {
$(this.options.selectAllLink, this.element).on('click', $.proxy(this._selectAllRelated, this));
$(this.options.relatedCheckbox, this.element).on('click', $.proxy(this._addRelatedToProduct, this));
+
+ if (this.element.data('shuffle')) {
+ this._shuffle(this.element.find(this.options.elementsSelector));
+ }
this._showRelatedProducts(
this.element.find(this.options.elementsSelector),
this.element.data('limit'),
- this.element.data('shuffle')
+ this.element.data('shuffle-weighted')
);
},
@@ -69,24 +73,66 @@ define([
);
},
+ /* jscs:disable */
+ /* eslint-disable */
/**
* Show related products according to limit. Shuffle if needed.
* @param {*} elements
* @param {*} limit
- * @param {*} shuffle
+ * @param weightedRandom
* @private
*/
- _showRelatedProducts: function (elements, limit, shuffle) {
- var index;
-
- if (shuffle) {
- this._shuffle(elements);
- }
+ _showRelatedProducts: function (elements, limit, weightedRandom) {
+ var index, weights = [], random = [], weight = 2, shown = 0, $element, currentGroup, prevGroup;
if (limit === 0) {
limit = elements.length;
}
+ if (weightedRandom && limit > 0 && limit < elements.length) {
+ for (index = 0; index < limit; index++) {
+ $element = $(elements[index]);
+ if ($element.data('shuffle-group') !== '') {
+ break;
+ }
+ $element.show();
+ shown++;
+ }
+ limit -= shown;
+ for (index = elements.length - 1; index >= 0; index--) {
+ $element = $(elements[index]);
+ currentGroup = $element.data('shuffle-group');
+ if (currentGroup !== '') {
+ weights.push([index, Math.log(weight)]);
+ if (typeof prevGroup !== 'undefined' && prevGroup !== currentGroup) {
+ weight += 2;
+ }
+ prevGroup = currentGroup;
+ }
+ }
+
+ if (weights.length === 0) {
+ return;
+ }
+
+ for (index = 0; index < weights.length; index++) {
+ random.push([weights[index][0], Math.pow(Math.random(), 1 / weights[index][1])]);
+ }
+
+ random.sort(function(a, b) {
+ a = a[1];
+ b = b[1];
+ return a < b ? 1 : (a > b ? -1 : 0);
+ });
+ index = 0;
+ while (limit) {
+ $(elements[random[index][0]]).show();
+ limit--;
+ index++
+ }
+ return;
+ }
+
for (index = 0; index < limit; index++) {
$(elements[index]).show();
}
@@ -96,12 +142,19 @@ define([
/* eslint-disable */
/**
* Shuffle an array
- * @param {Array} o
+ * @param {Array} elements
* @returns {*}
*/
- _shuffle: function shuffle(o) { //v1.0
- for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
- return o;
+ _shuffle: function shuffle(elements) {
+ var parent, child, lastSibling;
+ if (elements.length) {
+ parent = $(elements[0]).parent();
+ }
+ while (elements.length) {
+ child = elements.splice(Math.floor(Math.random() * elements.length), 1)[0];
+ lastSibling = parent.find('[data-shuffle-group="' + $(child).data('shuffle-group') + '"]').last();
+ lastSibling.after(child);
+ }
}
/* jscs:disable */
diff -Nuar a/vendor/magento/module-catalog/view/frontend/web/js/upsell-products.js b/vendor/magento/module-catalog/view/frontend/web/js/upsell-products.js
index da2526d5679..867011d3d16 100644
--- a/vendor/magento/module-catalog/view/frontend/web/js/upsell-products.js
+++ b/vendor/magento/module-catalog/view/frontend/web/js/upsell-products.js
@@ -19,33 +19,78 @@ define([
* @private
*/
_create: function () {
+ if (this.element.data('shuffle')) {
+ this._shuffle(this.element.find(this.options.elementsSelector));
+ }
this._showUpsellProducts(
this.element.find(this.options.elementsSelector),
this.element.data('limit'),
- this.element.data('shuffle')
+ this.element.data('shuffle-weighted')
);
},
+ /* jscs:disable */
+ /* eslint-disable */
/**
* Show upsell products according to limit. Shuffle if needed.
* @param {*} elements
* @param {Number} limit
- * @param {Boolean} shuffle
+ * @param {Boolean} weightedRandom
* @private
*/
- _showUpsellProducts: function (elements, limit, shuffle) {
- var index;
-
- if (shuffle) {
- this._shuffle(elements);
- }
+ _showUpsellProducts: function (elements, limit, weightedRandom) {
+ var index, weights = [], random = [], weight = 2, shown = 0, $element, currentGroup, prevGroup;
if (limit === 0) {
limit = elements.length;
}
+ if (weightedRandom && limit > 0 && limit < elements.length) {
+ for (index = 0; index < limit; index++) {
+ $element = $(elements[index]);
+ if ($element.data('shuffle-group') !== '') {
+ break;
+ }
+ $element.show();
+ shown++;
+ }
+ limit -= shown;
+ for (index = elements.length - 1; index >= 0; index--) {
+ $element = $(elements[index]);
+ currentGroup = $element.data('shuffle-group');
+ if (currentGroup !== '') {
+ weights.push([index, Math.log(weight)]);
+ if (typeof prevGroup !== 'undefined' && prevGroup !== currentGroup) {
+ weight += 2;
+ }
+ prevGroup = currentGroup;
+ }
+ }
+
+ if (weights.length === 0) {
+ return;
+ }
+
+ for (index = 0; index < weights.length; index++) {
+ random.push([weights[index][0], Math.pow(Math.random(), 1 / weights[index][1])]);
+ }
+
+ random.sort(function(a, b) {
+ a = a[1];
+ b = b[1];
+ return a < b ? 1 : (a > b ? -1 : 0);
+ });
+ index = 0;
+ while (limit) {
+ $(elements[random[index][0]]).show();
+ limit--;
+ index++
+ }
+ return;
+ }
+
for (index = 0; index < limit; index++) {
- $(this.element).find(elements[index]).show();
+ $(elements[index]).show();
}
},
@@ -53,12 +98,19 @@ define([
/* eslint-disable */
/**
* Shuffle an array
- * @param o
+ * @param elements
* @returns {*}
*/
- _shuffle: function shuffle(o){ //v1.0
- for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
- return o;
+ _shuffle: function shuffle(elements){ //v1.0
+ var parent, child, lastSibling;
+ if (elements.length) {
+ parent = $(elements[0]).parent();
+ }
+ while (elements.length) {
+ child = elements.splice(Math.floor(Math.random() * elements.length), 1)[0];
+ lastSibling = parent.find('[data-shuffle-group="' + $(child).data('shuffle-group') + '"]').last();
+ lastSibling.after(child);
+ }
}
/* jscs:disable */