Skip to content

Commit 3f15dc8

Browse files
ENGCOM-6602: #11209 Wishlist Add grouped product Error #26258
2 parents 199d7c2 + 0ace795 commit 3f15dc8

File tree

4 files changed

+290
-1
lines changed

4 files changed

+290
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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\GroupedProduct\Model\Wishlist\Product;
9+
10+
use Magento\Wishlist\Model\Item as WishlistItem;
11+
use Magento\GroupedProduct\Model\Product\Type\Grouped as TypeGrouped;
12+
use Magento\Catalog\Model\Product;
13+
14+
/**
15+
* Wishlist logic for grouped product
16+
*/
17+
class Item
18+
{
19+
/**
20+
* Modify Wishlist item based on associated product qty
21+
*
22+
* @param WishlistItem $subject
23+
* @param Product $product
24+
* @return array
25+
* @throws \Magento\Framework\Exception\LocalizedException
26+
*/
27+
public function beforeRepresentProduct(
28+
WishlistItem $subject,
29+
Product $product
30+
): array {
31+
if ($product->getTypeId() === TypeGrouped::TYPE_CODE
32+
&& $product->getId() === $subject->getProduct()->getId()
33+
) {
34+
$itemOptions = $subject->getOptionsByCode();
35+
$productOptions = $product->getCustomOptions();
36+
37+
$diff = array_diff_key($itemOptions, $productOptions);
38+
39+
if (!$diff) {
40+
$buyRequest = $subject->getBuyRequest();
41+
$superGroupInfo = $buyRequest->getData('super_group');
42+
43+
foreach ($itemOptions as $key => $itemOption) {
44+
if (preg_match('/associated_product_\d+/', $key)) {
45+
$simpleId = str_replace('associated_product_', '', $key);
46+
$prodQty = $productOptions[$key]->getValue();
47+
48+
$itemOption->setValue($itemOption->getValue() + $prodQty);
49+
50+
if (isset($superGroupInfo[$simpleId])) {
51+
$superGroupInfo[$simpleId] = $itemOptions[$key]->getValue();
52+
}
53+
}
54+
}
55+
56+
$buyRequest->setData('super_group', $superGroupInfo);
57+
58+
$subject->setOptions($itemOptions);
59+
$subject->mergeBuyRequest($buyRequest);
60+
}
61+
}
62+
63+
return [$product];
64+
}
65+
66+
/**
67+
* Remove associated_product_id key. associated_product_id contains qty
68+
*
69+
* @param WishlistItem $subject
70+
* @param array $options1
71+
* @param array $options2
72+
* @return array
73+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
74+
*/
75+
public function beforeCompareOptions(
76+
WishlistItem $subject,
77+
array $options1,
78+
array $options2
79+
): array {
80+
$diff = array_diff_key($options1, $options2);
81+
82+
if (!$diff) {
83+
foreach (array_keys($options1) as $key) {
84+
if (preg_match('/associated_product_\d+/', $key)) {
85+
unset($options1[$key]);
86+
unset($options2[$key]);
87+
}
88+
}
89+
}
90+
91+
return [$options1, $options2];
92+
}
93+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\GroupedProduct\Test\Unit\Model\Wishlist\Product;
8+
9+
use Magento\GroupedProduct\Model\Product\Type\Grouped as TypeGrouped;
10+
11+
/**
12+
* Unit test for Wishlist Item Plugin.
13+
*/
14+
class ItemTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/**
17+
* @var \Magento\GroupedProduct\Model\Wishlist\Product\Item
18+
*/
19+
protected $model;
20+
21+
/**
22+
* @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $productMock;
25+
26+
/**
27+
* @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $subjectMock;
30+
31+
/**
32+
* Init Mock Objects
33+
*/
34+
protected function setUp()
35+
{
36+
$this->subjectMock = $this->createPartialMock(
37+
\Magento\Wishlist\Model\Item::class,
38+
[
39+
'getOptionsByCode',
40+
'getBuyRequest',
41+
'setOptions',
42+
'mergeBuyRequest',
43+
'getProduct'
44+
]
45+
);
46+
47+
$this->productMock = $this->createPartialMock(
48+
\Magento\Catalog\Model\Product::class,
49+
[
50+
'getId',
51+
'getTypeId',
52+
'getCustomOptions'
53+
]
54+
);
55+
56+
$this->model = new \Magento\GroupedProduct\Model\Wishlist\Product\Item();
57+
}
58+
59+
/**
60+
* Test Before Represent Product method
61+
*/
62+
public function testBeforeRepresentProduct()
63+
{
64+
$testSimpleProdId = 34;
65+
$prodInitQty = 2;
66+
$prodQtyInWishlist = 3;
67+
$resWishlistQty = $prodInitQty + $prodQtyInWishlist;
68+
$superGroup = [
69+
'super_group' => [
70+
33 => "0",
71+
34 => 3,
72+
35 => "0"
73+
]
74+
];
75+
76+
$superGroupObj = new \Magento\Framework\DataObject($superGroup);
77+
78+
$this->productMock->expects($this->once())->method('getId')->willReturn($testSimpleProdId);
79+
$this->productMock->expects($this->once())->method('getTypeId')
80+
->willReturn(TypeGrouped::TYPE_CODE);
81+
$this->productMock->expects($this->once())->method('getCustomOptions')
82+
->willReturn(
83+
$this->getProductAssocOption($prodInitQty, $testSimpleProdId)
84+
);
85+
86+
$wishlistItemProductMock = $this->createPartialMock(
87+
\Magento\Catalog\Model\Product::class,
88+
[
89+
'getId',
90+
]
91+
);
92+
$wishlistItemProductMock->expects($this->once())->method('getId')->willReturn($testSimpleProdId);
93+
94+
$this->subjectMock->expects($this->once())->method('getProduct')
95+
->willReturn($wishlistItemProductMock);
96+
$this->subjectMock->expects($this->once())->method('getOptionsByCode')
97+
->willReturn(
98+
$this->getWishlistAssocOption($prodQtyInWishlist, $resWishlistQty, $testSimpleProdId)
99+
);
100+
$this->subjectMock->expects($this->once())->method('getBuyRequest')->willReturn($superGroupObj);
101+
102+
$this->model->beforeRepresentProduct($this->subjectMock, $this->productMock);
103+
}
104+
105+
/**
106+
* Test Before Compare Options method with same keys
107+
*/
108+
public function testBeforeCompareOptionsSameKeys()
109+
{
110+
$options1 = ['associated_product_34' => 3];
111+
$options2 = ['associated_product_34' => 2];
112+
113+
$res = $this->model->beforeCompareOptions($this->subjectMock, $options1, $options2);
114+
115+
$this->assertEquals([], $res[0]);
116+
$this->assertEquals([], $res[1]);
117+
}
118+
119+
/**
120+
* Test Before Compare Options method with diff keys
121+
*/
122+
public function testBeforeCompareOptionsDiffKeys()
123+
{
124+
$options1 = ['associated_product_1' => 3];
125+
$options2 = ['associated_product_34' => 2];
126+
127+
$res = $this->model->beforeCompareOptions($this->subjectMock, $options1, $options2);
128+
129+
$this->assertEquals($options1, $res[0]);
130+
$this->assertEquals($options2, $res[1]);
131+
}
132+
133+
/**
134+
* Return mock array with wishlist options
135+
*
136+
* @param int $initVal
137+
* @param int $resVal
138+
* @param int $prodId
139+
* @return array
140+
*/
141+
private function getWishlistAssocOption($initVal, $resVal, $prodId)
142+
{
143+
$items = [];
144+
145+
$optionMock = $this->createPartialMock(
146+
\Magento\Wishlist\Model\Item\Option::class,
147+
[
148+
'getValue',
149+
]
150+
);
151+
$optionMock->expects($this->at(0))->method('getValue')->willReturn($initVal);
152+
$optionMock->expects($this->at(1))->method('getValue')->willReturn($resVal);
153+
154+
$items['associated_product_' . $prodId] = $optionMock;
155+
156+
return $items;
157+
}
158+
159+
/**
160+
* Return mock array with product options
161+
*
162+
* @param int $initVal
163+
* @param int $prodId
164+
* @return array
165+
*/
166+
private function getProductAssocOption($initVal, $prodId)
167+
{
168+
$items = [];
169+
170+
$optionMock = $this->createPartialMock(
171+
\Magento\Catalog\Model\Product\Configuration\Item\Option::class,
172+
[
173+
'getValue',
174+
]
175+
);
176+
177+
$optionMock->expects($this->once())->method('getValue')->willReturn($initVal);
178+
179+
$items['associated_product_' . $prodId] = $optionMock;
180+
181+
return $items;
182+
}
183+
}

app/code/Magento/GroupedProduct/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"magento/module-quote": "*",
1919
"magento/module-sales": "*",
2020
"magento/module-store": "*",
21-
"magento/module-ui": "*"
21+
"magento/module-ui": "*",
22+
"magento/module-wishlist": "*"
2223
},
2324
"suggest": {
2425
"magento/module-grouped-product-sample-data": "*"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Wishlist\Model\Item">
10+
<plugin name="groupedProductWishlistProcessor" type="Magento\GroupedProduct\Model\Wishlist\Product\Item" />
11+
</type>
12+
</config>

0 commit comments

Comments
 (0)