Skip to content

Commit 677f1b6

Browse files
authored
Merge pull request #3915 from magento-epam/EPAM-PR-47
- Fixed "My Wishlist - quantity input box issue" - [GitHub] Can't use "configurable" as the group name in attribute sets M2.1 #6123
2 parents ff3c9b3 + 277dba9 commit 677f1b6

File tree

7 files changed

+128
-19
lines changed

7 files changed

+128
-19
lines changed

app/code/Magento/ConfigurableProduct/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,11 @@
248248
<type name="Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector">
249249
<plugin name="apply_tax_class_id" type="Magento\ConfigurableProduct\Plugin\Tax\Model\Sales\Total\Quote\CommonTaxCollector" />
250250
</type>
251+
<type name="Magento\Eav\Model\Entity\Attribute\Group">
252+
<arguments>
253+
<argument name="reservedSystemNames" xsi:type="array">
254+
<item name="configurable" xsi:type="string">configurable</item>
255+
</argument>
256+
</arguments>
257+
</type>
251258
</config>

app/code/Magento/Eav/Model/Entity/Attribute/Group.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Eav\Model\Entity\Attribute;
78

9+
use Magento\Eav\Api\Data\AttributeGroupExtensionInterface;
810
use Magento\Framework\Api\AttributeValueFactory;
11+
use Magento\Framework\Exception\LocalizedException;
912

1013
/**
14+
* Entity attribute group model
15+
*
1116
* @api
1217
* @method int getSortOrder()
1318
* @method \Magento\Eav\Model\Entity\Attribute\Group setSortOrder(int $value)
@@ -27,6 +32,11 @@ class Group extends \Magento\Framework\Model\AbstractExtensibleModel implements
2732
*/
2833
private $translitFilter;
2934

35+
/**
36+
* @var array
37+
*/
38+
private $reservedSystemNames = [];
39+
3040
/**
3141
* @param \Magento\Framework\Model\Context $context
3242
* @param \Magento\Framework\Registry $registry
@@ -35,7 +45,8 @@ class Group extends \Magento\Framework\Model\AbstractExtensibleModel implements
3545
* @param \Magento\Framework\Filter\Translit $translitFilter
3646
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
3747
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
38-
* @param array $data
48+
* @param array $data (optional)
49+
* @param array $reservedSystemNames (optional)
3950
*/
4051
public function __construct(
4152
\Magento\Framework\Model\Context $context,
@@ -45,7 +56,8 @@ public function __construct(
4556
\Magento\Framework\Filter\Translit $translitFilter,
4657
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
4758
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
48-
array $data = []
59+
array $data = [],
60+
array $reservedSystemNames = []
4961
) {
5062
parent::__construct(
5163
$context,
@@ -56,6 +68,7 @@ public function __construct(
5668
$resourceCollection,
5769
$data
5870
);
71+
$this->reservedSystemNames = $reservedSystemNames;
5972
$this->translitFilter = $translitFilter;
6073
}
6174

@@ -74,6 +87,7 @@ protected function _construct()
7487
* Checks if current attribute group exists
7588
*
7689
* @return bool
90+
* @throws LocalizedException
7791
* @codeCoverageIgnore
7892
*/
7993
public function itemExists()
@@ -85,6 +99,7 @@ public function itemExists()
8599
* Delete groups
86100
*
87101
* @return $this
102+
* @throws LocalizedException
88103
* @codeCoverageIgnore
89104
*/
90105
public function deleteGroups()
@@ -110,9 +125,10 @@ public function beforeSave()
110125
),
111126
'-'
112127
);
113-
if (empty($attributeGroupCode)) {
128+
$isReservedSystemName = in_array(strtolower($attributeGroupCode), $this->reservedSystemNames);
129+
if (empty($attributeGroupCode) || $isReservedSystemName) {
114130
// in the following code md5 is not used for security purposes
115-
$attributeGroupCode = md5($groupName);
131+
$attributeGroupCode = md5(strtolower($groupName));
116132
}
117133
$this->setAttributeGroupCode($attributeGroupCode);
118134
}
@@ -121,7 +137,8 @@ public function beforeSave()
121137
}
122138

123139
/**
124-
* {@inheritdoc}
140+
* @inheritdoc
141+
*
125142
* @codeCoverageIgnoreStart
126143
*/
127144
public function getAttributeGroupId()
@@ -130,64 +147,63 @@ public function getAttributeGroupId()
130147
}
131148

132149
/**
133-
* {@inheritdoc}
150+
* @inheritdoc
134151
*/
135152
public function getAttributeGroupName()
136153
{
137154
return $this->getData(self::GROUP_NAME);
138155
}
139156

140157
/**
141-
* {@inheritdoc}
158+
* @inheritdoc
142159
*/
143160
public function getAttributeSetId()
144161
{
145162
return $this->getData(self::ATTRIBUTE_SET_ID);
146163
}
147164

148165
/**
149-
* {@inheritdoc}
166+
* @inheritdoc
150167
*/
151168
public function setAttributeGroupId($attributeGroupId)
152169
{
153170
return $this->setData(self::GROUP_ID, $attributeGroupId);
154171
}
155172

156173
/**
157-
* {@inheritdoc}
174+
* @inheritdoc
158175
*/
159176
public function setAttributeGroupName($attributeGroupName)
160177
{
161178
return $this->setData(self::GROUP_NAME, $attributeGroupName);
162179
}
163180

164181
/**
165-
* {@inheritdoc}
182+
* @inheritdoc
166183
*/
167184
public function setAttributeSetId($attributeSetId)
168185
{
169186
return $this->setData(self::ATTRIBUTE_SET_ID, $attributeSetId);
170187
}
171188

172189
/**
173-
* {@inheritdoc}
190+
* @inheritdoc
174191
*
175-
* @return \Magento\Eav\Api\Data\AttributeGroupExtensionInterface|null
192+
* @return AttributeGroupExtensionInterface|null
176193
*/
177194
public function getExtensionAttributes()
178195
{
179196
return $this->_getExtensionAttributes();
180197
}
181198

182199
/**
183-
* {@inheritdoc}
200+
* @inheritdoc
184201
*
185-
* @param \Magento\Eav\Api\Data\AttributeGroupExtensionInterface $extensionAttributes
202+
* @param AttributeGroupExtensionInterface $extensionAttributes
186203
* @return $this
187204
*/
188-
public function setExtensionAttributes(
189-
\Magento\Eav\Api\Data\AttributeGroupExtensionInterface $extensionAttributes
190-
) {
205+
public function setExtensionAttributes(AttributeGroupExtensionInterface $extensionAttributes)
206+
{
191207
return $this->_setExtensionAttributes($extensionAttributes);
192208
}
193209

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/GroupTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected function setUp()
4040
'resource' => $this->resourceMock,
4141
'translitFilter' => $translitFilter,
4242
'context' => $contextMock,
43+
'reservedSystemNames' => ['configurable'],
4344
];
4445
$objectManager = new ObjectManager($this);
4546
$this->model = $objectManager->getObject(
@@ -67,6 +68,8 @@ public function attributeGroupCodeDataProvider()
6768
{
6869
return [
6970
['General Group', 'general-group'],
71+
['configurable', md5('configurable')],
72+
['configurAble', md5('configurable')],
7073
['///', md5('///')],
7174
];
7275
}

app/code/Magento/Eav/etc/di.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,3 @@
210210
</arguments>
211211
</type>
212212
</config>
213-
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Wishlist\ViewModel;
9+
10+
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter;
11+
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
12+
use Magento\CatalogInventory\Model\StockRegistry;
13+
use Magento\Framework\View\Element\Block\ArgumentInterface;
14+
15+
/**
16+
* ViewModel for Wishlist Cart Block
17+
*/
18+
class AllowedQuantity implements ArgumentInterface
19+
{
20+
/**
21+
* @var StockRegistry
22+
*/
23+
private $stockRegistry;
24+
25+
/**
26+
* @var ItemInterface
27+
*/
28+
private $item;
29+
30+
/**
31+
* @param StockRegistry $stockRegistry
32+
*/
33+
public function __construct(StockRegistry $stockRegistry)
34+
{
35+
$this->stockRegistry = $stockRegistry;
36+
}
37+
38+
/**
39+
* Set product configuration item
40+
*
41+
* @param ItemInterface $item
42+
* @return self
43+
*/
44+
public function setItem(ItemInterface $item): self
45+
{
46+
$this->item = $item;
47+
return $this;
48+
}
49+
50+
/**
51+
* Get product configuration item
52+
*
53+
* @return ItemInterface
54+
*/
55+
public function getItem(): ItemInterface
56+
{
57+
return $this->item;
58+
}
59+
60+
/**
61+
* Get min and max qty for wishlist form.
62+
*
63+
* @return array
64+
*/
65+
public function getMinMaxQty(): array
66+
{
67+
$product = $this->getItem()->getProduct();
68+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
69+
$params = [];
70+
71+
$params['minAllowed'] = (float)$stockItem->getMinSaleQty();
72+
if ($stockItem->getMaxSaleQty()) {
73+
$params['maxAllowed'] = (float)$stockItem->getMaxSaleQty();
74+
} else {
75+
$params['maxAllowed'] = (float)StockDataFilter::MAX_QTY_VALUE;
76+
}
77+
78+
return $params;
79+
}
80+
}

app/code/Magento/Wishlist/view/frontend/layout/wishlist_index_index.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
</block>
4242
<block class="Magento\Wishlist\Block\Customer\Wishlist\Item\Column\Cart" name="customer.wishlist.item.cart" template="Magento_Wishlist::item/column/cart.phtml" cacheable="false">
4343
<arguments>
44+
<argument name="allowedQuantityViewModel" xsi:type="object">Magento\Wishlist\ViewModel\AllowedQuantity</argument>
4445
<argument name="title" translate="true" xsi:type="string">Add to Cart</argument>
4546
</arguments>
4647
</block>

app/code/Magento/Wishlist/view/frontend/templates/item/column/cart.phtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
/** @var \Magento\Wishlist\Model\Item $item */
1212
$item = $block->getItem();
1313
$product = $item->getProduct();
14+
/** @var \Magento\Wishlist\ViewModel\AllowedQuantity $viewModel */
15+
$viewModel = $block->getData('allowedQuantityViewModel');
16+
$allowedQty = $viewModel->setItem($item)->getMinMaxQty();
1417
?>
1518
<?php foreach ($block->getChildNames() as $childName): ?>
1619
<?= /* @noEscape */ $block->getLayout()->renderElement($childName, false) ?>
@@ -21,7 +24,7 @@ $product = $item->getProduct();
2124
<div class="field qty">
2225
<label class="label" for="qty[<?= $block->escapeHtmlAttr($item->getId()) ?>]"><span><?= $block->escapeHtml(__('Qty')) ?></span></label>
2326
<div class="control">
24-
<input type="number" data-role="qty" id="qty[<?= $block->escapeHtmlAttr($item->getId()) ?>]" class="input-text qty" data-validate="{'required-number':true,'validate-greater-than-zero':true}"
27+
<input type="number" data-role="qty" id="qty[<?= $block->escapeHtmlAttr($item->getId()) ?>]" class="input-text qty" data-validate="{'required-number':true,'validate-greater-than-zero':true, 'validate-item-quantity':{'minAllowed':<?= /* @noEscape */ $allowedQty['minAllowed'] ?>,'maxAllowed':<?= /* @noEscape */ $allowedQty['maxAllowed'] ?>}}"
2528
name="qty[<?= $block->escapeHtmlAttr($item->getId()) ?>]" value="<?= /* @noEscape */ (int)($block->getAddToCartQty($item) * 1) ?>">
2629
</div>
2730
</div>

0 commit comments

Comments
 (0)