Skip to content

Commit 5067755

Browse files
authored
Merge pull request #5086 from magento-mpi/PR-2019-12-06
MPI stories 2.4
2 parents 03f3a68 + de676ee commit 5067755

File tree

20 files changed

+837
-174
lines changed

20 files changed

+837
-174
lines changed

app/code/Magento/Catalog/Helper/Product/Edit/Action/Attribute.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

7-
/**
8-
* Adminhtml catalog product action attribute update helper
9-
*/
108
namespace Magento\Catalog\Helper\Product\Edit\Action;
119

1210
/**
13-
* Class Attribute
11+
* Adminhtml catalog product action attribute update helper.
12+
*
1413
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1515
*/
1616
class Attribute extends \Magento\Backend\Helper\Data
1717
{
@@ -32,7 +32,7 @@ class Attribute extends \Magento\Backend\Helper\Data
3232
/**
3333
* Excluded from batch update attribute codes
3434
*
35-
* @var string[]
35+
* @var array
3636
*/
3737
protected $_excludedAttributes = ['url_key'];
3838

@@ -92,6 +92,7 @@ public function __construct(
9292

9393
/**
9494
* Return product collection with selected product filter
95+
*
9596
* Product collection didn't load
9697
*
9798
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
@@ -171,8 +172,8 @@ public function getAttributes()
171172
$this->getProductsSetIds()
172173
);
173174

174-
if ($this->_excludedAttributes) {
175-
$this->_attributes->addFieldToFilter('attribute_code', ['nin' => $this->_excludedAttributes]);
175+
if ($excludedAttributes = $this->getExcludedAttributes()) {
176+
$this->_attributes->addFieldToFilter('attribute_code', ['nin' => $excludedAttributes]);
176177
}
177178

178179
// check product type apply to limitation and remove attributes that impossible to change in mass-update
@@ -193,11 +194,24 @@ public function getAttributes()
193194
}
194195

195196
/**
197+
* Gets website id.
198+
*
196199
* @param int $storeId
197200
* @return int
201+
* @throws \Magento\Framework\Exception\NoSuchEntityException
198202
*/
199203
public function getStoreWebsiteId($storeId)
200204
{
201205
return $this->_storeManager->getStore($storeId)->getWebsiteId();
202206
}
207+
208+
/**
209+
* Retrieve excluded attributes.
210+
*
211+
* @return array
212+
*/
213+
public function getExcludedAttributes(): array
214+
{
215+
return $this->_excludedAttributes;
216+
}
203217
}

app/code/Magento/Catalog/view/frontend/templates/product/list/items.phtml

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis
88
// phpcs:disable Generic.WhiteSpace.ScopeIndent.Incorrect
9+
// phpcs:disable Generic.Files.LineLength
10+
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundHelper
911

1012
/* @var $block \Magento\Catalog\Block\Product\AbstractProduct */
1113
?>
@@ -23,6 +25,7 @@ switch ($type = $block->getType()) {
2325
$items = $block->getAllItems();
2426
$limit = $block->getPositionLimit();
2527
$shuffle = (int) $block->isShuffled();
28+
$isWeightedRandom = (int) $block->getRotation()->isWeightedRandom($block->getProductListType());
2629
$canItemsAddToCart = $block->canItemsAddToCart();
2730

2831
$showAddTo = true;
@@ -43,6 +46,7 @@ switch ($type = $block->getType()) {
4346
$items = $block->getItems();
4447
$limit = 0;
4548
$shuffle = 0;
49+
$isWeightedRandom = 0;
4650
$canItemsAddToCart = $block->canItemsAddToCart();
4751

4852
$showAddTo = true;
@@ -62,6 +66,7 @@ switch ($type = $block->getType()) {
6266
$items = $block->getAllItems();
6367
$limit = $block->getPositionLimit();
6468
$shuffle = (int) $block->isShuffled();
69+
$isWeightedRandom = (int) $block->getRotation()->isWeightedRandom($block->getProductListType());
6570

6671
$showAddTo = false;
6772
$showCart = false;
@@ -82,6 +87,7 @@ switch ($type = $block->getType()) {
8287
$items = $block->getItemCollection()->getItems();
8388
$limit = $block->getItemLimit('upsell');
8489
$shuffle = 0;
90+
$isWeightedRandom = 0;
8591

8692
$showAddTo = false;
8793
$showCart = false;
@@ -152,39 +158,39 @@ switch ($type = $block->getType()) {
152158
}
153159
?>
154160

155-
<?php if ($exist) :?>
161+
<?php if ($exist):?>
156162

157-
<?php if ($type == 'related' || $type == 'upsell') :?>
158-
<?php if ($type == 'related') :?>
159-
<div class="block <?= $block->escapeHtmlAttr($class) ?>" data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}' data-limit="<?= $block->escapeHtmlAttr($limit) ?>" data-shuffle="<?= /* @noEscape */ $shuffle ?>">
160-
<?php else :?>
161-
<div class="block <?= $block->escapeHtmlAttr($class) ?>" data-mage-init='{"upsellProducts":{}}' data-limit="<?= $block->escapeHtmlAttr($limit) ?>" data-shuffle="<?= /* @noEscape */ $shuffle ?>">
163+
<?php if ($type == 'related' || $type == 'upsell'):?>
164+
<?php if ($type == 'related'):?>
165+
<div class="block <?= $block->escapeHtmlAttr($class) ?>" data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}' data-limit="<?= $block->escapeHtmlAttr($limit) ?>" data-shuffle="<?= /* @noEscape */ $shuffle ?>" data-shuffle-weighted="<?= /* @noEscape */ $isWeightedRandom ?>">
166+
<?php else:?>
167+
<div class="block <?= $block->escapeHtmlAttr($class) ?>" data-mage-init='{"upsellProducts":{}}' data-limit="<?= $block->escapeHtmlAttr($limit) ?>" data-shuffle="<?= /* @noEscape */ $shuffle ?>" data-shuffle-weighted="<?= /* @noEscape */ $isWeightedRandom ?>">
162168
<?php endif; ?>
163-
<?php else :?>
169+
<?php else:?>
164170
<div class="block <?= $block->escapeHtmlAttr($class) ?>">
165171
<?php endif; ?>
166172
<div class="block-title title">
167173
<strong id="block-<?= $block->escapeHtmlAttr($class) ?>-heading" role="heading" aria-level="2"><?= $block->escapeHtml($title) ?></strong>
168174
</div>
169175
<div class="block-content content" aria-labelledby="block-<?= $block->escapeHtmlAttr($class) ?>-heading">
170-
<?php if ($type == 'related' && $canItemsAddToCart) :?>
176+
<?php if ($type == 'related' && $canItemsAddToCart):?>
171177
<div class="block-actions">
172178
<?= $block->escapeHtml(__('Check items to add to the cart or')) ?>
173179
<button type="button" class="action select" data-role="select-all"><span><?= $block->escapeHtml(__('select all')) ?></span></button>
174180
</div>
175181
<?php endif; ?>
176182
<div class="products wrapper grid products-grid products-<?= $block->escapeHtmlAttr($type) ?>">
177183
<ol class="products list items product-items">
178-
<?php foreach ($items as $_item) :?>
184+
<?php foreach ($items as $_item):?>
179185
<?php $available = ''; ?>
180-
<?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related') :?>
181-
<?php if (!$_item->getRequiredOptions()) :?>
186+
<?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'):?>
187+
<?php if (!$_item->getRequiredOptions()):?>
182188
<?php $available = 'related-available'; ?>
183189
<?php endif; ?>
184190
<?php endif; ?>
185-
<?php if ($type == 'related' || $type == 'upsell') :?>
186-
<li class="item product product-item" style="display: none;">
187-
<?php else :?>
191+
<?php if ($type == 'related' || $type == 'upsell'):?>
192+
<li class="item product product-item" style="display: none;" data-shuffle-group="<?= $block->escapeHtmlAttr($_item->getPriority()) ?>" >
193+
<?php else:?>
188194
<li class="item product product-item">
189195
<?php endif; ?>
190196
<div class="product-item-info <?= /* @noEscape */ $available ?>">
@@ -199,29 +205,29 @@ switch ($type = $block->getType()) {
199205

200206
<?= /* @noEscape */ $block->getProductPrice($_item) ?>
201207

202-
<?php if ($templateType) :?>
208+
<?php if ($templateType):?>
203209
<?= $block->getReviewsSummaryHtml($_item, $templateType) ?>
204210
<?php endif; ?>
205211

206-
<?php if ($canItemsAddToCart && !$_item->isComposite() && $_item->isSaleable() && $type == 'related') :?>
207-
<?php if (!$_item->getRequiredOptions()) :?>
212+
<?php if ($canItemsAddToCart && !$_item->isComposite() && $_item->isSaleable() && $type == 'related'):?>
213+
<?php if (!$_item->getRequiredOptions()):?>
208214
<div class="field choice related">
209215
<input type="checkbox" class="checkbox related" id="related-checkbox<?= $block->escapeHtmlAttr($_item->getId()) ?>" name="related_products[]" value="<?= $block->escapeHtmlAttr($_item->getId()) ?>" />
210216
<label class="label" for="related-checkbox<?= $block->escapeHtmlAttr($_item->getId()) ?>"><span><?= $block->escapeHtml(__('Add to Cart')) ?></span></label>
211217
</div>
212218
<?php endif; ?>
213219
<?php endif; ?>
214220

215-
<?php if ($showAddTo || $showCart) :?>
221+
<?php if ($showAddTo || $showCart):?>
216222
<div class="product actions product-item-actions">
217-
<?php if ($showCart) :?>
223+
<?php if ($showCart):?>
218224
<div class="actions-primary">
219-
<?php if ($_item->isSaleable()) :?>
220-
<?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)) :?>
225+
<?php if ($_item->isSaleable()):?>
226+
<?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)):?>
221227
<button class="action tocart primary" data-mage-init='{"redirectUrl": {"url": "<?= $block->escapeUrl($block->getAddToCartUrl($_item)) ?>"}}' type="button" title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>">
222228
<span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
223229
</button>
224-
<?php else :?>
230+
<?php else:?>
225231
<?php $postDataHelper = $this->helper(Magento\Framework\Data\Helper\PostHelper::class);
226232
$postData = $postDataHelper->getPostData($block->escapeUrl($block->getAddToCartUrl($_item)), ['product' => $_item->getEntityId()])
227233
?>
@@ -231,19 +237,19 @@ switch ($type = $block->getType()) {
231237
<span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
232238
</button>
233239
<?php endif; ?>
234-
<?php else :?>
235-
<?php if ($_item->getIsSalable()) :?>
240+
<?php else:?>
241+
<?php if ($_item->getIsSalable()):?>
236242
<div class="stock available"><span><?= $block->escapeHtml(__('In stock')) ?></span></div>
237-
<?php else :?>
243+
<?php else:?>
238244
<div class="stock unavailable"><span><?= $block->escapeHtml(__('Out of stock')) ?></span></div>
239245
<?php endif; ?>
240246
<?php endif; ?>
241247
</div>
242248
<?php endif; ?>
243249

244-
<?php if ($showAddTo) :?>
250+
<?php if ($showAddTo):?>
245251
<div class="secondary-addto-links actions-secondary" data-role="add-to-links">
246-
<?php if ($addToBlock = $block->getChildBlock('addto')) :?>
252+
<?php if ($addToBlock = $block->getChildBlock('addto')):?>
247253
<?= $addToBlock->setProduct($_item)->getChildHtml() ?>
248254
<?php endif; ?>
249255
</div>

app/code/Magento/Catalog/view/frontend/web/js/related-products.js

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ define([
2828
_create: function () {
2929
$(this.options.selectAllLink, this.element).on('click', $.proxy(this._selectAllRelated, this));
3030
$(this.options.relatedCheckbox, this.element).on('click', $.proxy(this._addRelatedToProduct, this));
31+
32+
if (this.element.data('shuffle')) {
33+
this._shuffle(this.element.find(this.options.elementsSelector));
34+
}
3135
this._showRelatedProducts(
3236
this.element.find(this.options.elementsSelector),
3337
this.element.data('limit'),
34-
this.element.data('shuffle')
38+
this.element.data('shuffle-weighted')
3539
);
3640
},
3741

@@ -69,24 +73,66 @@ define([
6973
);
7074
},
7175

76+
/* jscs:disable */
77+
/* eslint-disable */
7278
/**
7379
* Show related products according to limit. Shuffle if needed.
7480
* @param {*} elements
7581
* @param {*} limit
76-
* @param {*} shuffle
82+
* @param weightedRandom
7783
* @private
7884
*/
79-
_showRelatedProducts: function (elements, limit, shuffle) {
80-
var index;
81-
82-
if (shuffle) {
83-
this._shuffle(elements);
84-
}
85+
_showRelatedProducts: function (elements, limit, weightedRandom) {
86+
var index, weights = [], random = [], weight = 2, shown = 0, $element, currentGroup, prevGroup;
8587

8688
if (limit === 0) {
8789
limit = elements.length;
8890
}
8991

92+
if (weightedRandom && limit > 0 && limit < elements.length) {
93+
for (index = 0; index < limit; index++) {
94+
$element = $(elements[index]);
95+
if ($element.data('shuffle-group') !== '') {
96+
break;
97+
}
98+
$element.show();
99+
shown++;
100+
}
101+
limit -= shown;
102+
for (index = elements.length - 1; index >= 0; index--) {
103+
$element = $(elements[index]);
104+
currentGroup = $element.data('shuffle-group');
105+
if (currentGroup !== '') {
106+
weights.push([index, Math.log(weight)]);
107+
if (typeof prevGroup !== 'undefined' && prevGroup !== currentGroup) {
108+
weight += 2;
109+
}
110+
prevGroup = currentGroup;
111+
}
112+
}
113+
114+
if (weights.length === 0) {
115+
return;
116+
}
117+
118+
for (index = 0; index < weights.length; index++) {
119+
random.push([weights[index][0], Math.pow(Math.random(), 1 / weights[index][1])]);
120+
}
121+
122+
random.sort(function(a, b) {
123+
a = a[1];
124+
b = b[1];
125+
return a < b ? 1 : (a > b ? -1 : 0);
126+
});
127+
index = 0;
128+
while (limit) {
129+
$(elements[random[index][0]]).show();
130+
limit--;
131+
index++
132+
}
133+
return;
134+
}
135+
90136
for (index = 0; index < limit; index++) {
91137
$(elements[index]).show();
92138
}
@@ -96,12 +142,19 @@ define([
96142
/* eslint-disable */
97143
/**
98144
* Shuffle an array
99-
* @param {Array} o
145+
* @param {Array} elements
100146
* @returns {*}
101147
*/
102-
_shuffle: function shuffle(o) { //v1.0
103-
for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
104-
return o;
148+
_shuffle: function shuffle(elements) {
149+
var parent, child, lastSibling;
150+
if (elements.length) {
151+
parent = $(elements[0]).parent();
152+
}
153+
while (elements.length) {
154+
child = elements.splice(Math.floor(Math.random() * elements.length), 1)[0];
155+
lastSibling = parent.find('[data-shuffle-group="' + $(child).data('shuffle-group') + '"]').last();
156+
lastSibling.after(child);
157+
}
105158
}
106159

107160
/* jscs:disable */

0 commit comments

Comments
 (0)