Skip to content

Commit 9a4ccb7

Browse files
11209-wishlist-add-grouped-product-error
1 parent 57a2aad commit 9a4ccb7

File tree

3 files changed

+281
-0
lines changed

3 files changed

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