-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Merge Pull Request #26258 from MaxRomanov4669/magento2:11209-wishlist-add-grouped-product-error - Merged commits: 1. 9a4ccb7 2. ec8a756 3. 6d7b457
- Loading branch information
Showing
4 changed files
with
290 additions
and
1 deletion.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
app/code/Magento/GroupedProduct/Model/Wishlist/Product/Item.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GroupedProduct\Model\Wishlist\Product; | ||
|
||
use Magento\Wishlist\Model\Item as WishlistItem; | ||
use Magento\GroupedProduct\Model\Product\Type\Grouped as TypeGrouped; | ||
use Magento\Catalog\Model\Product; | ||
|
||
/** | ||
* Wishlist logic for grouped product | ||
*/ | ||
class Item | ||
{ | ||
/** | ||
* Modify Wishlist item based on associated product qty | ||
* | ||
* @param WishlistItem $subject | ||
* @param Product $product | ||
* @return array | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function beforeRepresentProduct( | ||
WishlistItem $subject, | ||
Product $product | ||
): array { | ||
if ($product->getTypeId() === TypeGrouped::TYPE_CODE | ||
&& $product->getId() === $subject->getProduct()->getId() | ||
) { | ||
$itemOptions = $subject->getOptionsByCode(); | ||
$productOptions = $product->getCustomOptions(); | ||
|
||
$diff = array_diff_key($itemOptions, $productOptions); | ||
|
||
if (!$diff) { | ||
$buyRequest = $subject->getBuyRequest(); | ||
$superGroupInfo = $buyRequest->getData('super_group'); | ||
|
||
foreach ($itemOptions as $key => $itemOption) { | ||
if (preg_match('/associated_product_\d+/', $key)) { | ||
$simpleId = str_replace('associated_product_', '', $key); | ||
$prodQty = $productOptions[$key]->getValue(); | ||
|
||
$itemOption->setValue($itemOption->getValue() + $prodQty); | ||
|
||
if (isset($superGroupInfo[$simpleId])) { | ||
$superGroupInfo[$simpleId] = $itemOptions[$key]->getValue(); | ||
} | ||
} | ||
} | ||
|
||
$buyRequest->setData('super_group', $superGroupInfo); | ||
|
||
$subject->setOptions($itemOptions); | ||
$subject->mergeBuyRequest($buyRequest); | ||
} | ||
} | ||
|
||
return [$product]; | ||
} | ||
|
||
/** | ||
* Remove associated_product_id key. associated_product_id contains qty | ||
* | ||
* @param WishlistItem $subject | ||
* @param array $options1 | ||
* @param array $options2 | ||
* @return array | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeCompareOptions( | ||
WishlistItem $subject, | ||
array $options1, | ||
array $options2 | ||
): array { | ||
$diff = array_diff_key($options1, $options2); | ||
|
||
if (!$diff) { | ||
foreach (array_keys($options1) as $key) { | ||
if (preg_match('/associated_product_\d+/', $key)) { | ||
unset($options1[$key]); | ||
unset($options2[$key]); | ||
} | ||
} | ||
} | ||
|
||
return [$options1, $options2]; | ||
} | ||
} |
183 changes: 183 additions & 0 deletions
183
app/code/Magento/GroupedProduct/Test/Unit/Model/Wishlist/Product/ItemTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<?php | ||
/** | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\GroupedProduct\Test\Unit\Model\Wishlist\Product; | ||
|
||
use Magento\GroupedProduct\Model\Product\Type\Grouped as TypeGrouped; | ||
|
||
/** | ||
* Unit test for Wishlist Item Plugin. | ||
*/ | ||
class ItemTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var \Magento\GroupedProduct\Model\Wishlist\Product\Item | ||
*/ | ||
protected $model; | ||
|
||
/** | ||
* @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $productMock; | ||
|
||
/** | ||
* @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $subjectMock; | ||
|
||
/** | ||
* Init Mock Objects | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->subjectMock = $this->createPartialMock( | ||
\Magento\Wishlist\Model\Item::class, | ||
[ | ||
'getOptionsByCode', | ||
'getBuyRequest', | ||
'setOptions', | ||
'mergeBuyRequest', | ||
'getProduct' | ||
] | ||
); | ||
|
||
$this->productMock = $this->createPartialMock( | ||
\Magento\Catalog\Model\Product::class, | ||
[ | ||
'getId', | ||
'getTypeId', | ||
'getCustomOptions' | ||
] | ||
); | ||
|
||
$this->model = new \Magento\GroupedProduct\Model\Wishlist\Product\Item(); | ||
} | ||
|
||
/** | ||
* Test Before Represent Product method | ||
*/ | ||
public function testBeforeRepresentProduct() | ||
{ | ||
$testSimpleProdId = 34; | ||
$prodInitQty = 2; | ||
$prodQtyInWishlist = 3; | ||
$resWishlistQty = $prodInitQty + $prodQtyInWishlist; | ||
$superGroup = [ | ||
'super_group' => [ | ||
33 => "0", | ||
34 => 3, | ||
35 => "0" | ||
] | ||
]; | ||
|
||
$superGroupObj = new \Magento\Framework\DataObject($superGroup); | ||
|
||
$this->productMock->expects($this->once())->method('getId')->willReturn($testSimpleProdId); | ||
$this->productMock->expects($this->once())->method('getTypeId') | ||
->willReturn(TypeGrouped::TYPE_CODE); | ||
$this->productMock->expects($this->once())->method('getCustomOptions') | ||
->willReturn( | ||
$this->getProductAssocOption($prodInitQty, $testSimpleProdId) | ||
); | ||
|
||
$wishlistItemProductMock = $this->createPartialMock( | ||
\Magento\Catalog\Model\Product::class, | ||
[ | ||
'getId', | ||
] | ||
); | ||
$wishlistItemProductMock->expects($this->once())->method('getId')->willReturn($testSimpleProdId); | ||
|
||
$this->subjectMock->expects($this->once())->method('getProduct') | ||
->willReturn($wishlistItemProductMock); | ||
$this->subjectMock->expects($this->once())->method('getOptionsByCode') | ||
->willReturn( | ||
$this->getWishlistAssocOption($prodQtyInWishlist, $resWishlistQty, $testSimpleProdId) | ||
); | ||
$this->subjectMock->expects($this->once())->method('getBuyRequest')->willReturn($superGroupObj); | ||
|
||
$this->model->beforeRepresentProduct($this->subjectMock, $this->productMock); | ||
} | ||
|
||
/** | ||
* Test Before Compare Options method with same keys | ||
*/ | ||
public function testBeforeCompareOptionsSameKeys() | ||
{ | ||
$options1 = ['associated_product_34' => 3]; | ||
$options2 = ['associated_product_34' => 2]; | ||
|
||
$res = $this->model->beforeCompareOptions($this->subjectMock, $options1, $options2); | ||
|
||
$this->assertEquals([], $res[0]); | ||
$this->assertEquals([], $res[1]); | ||
} | ||
|
||
/** | ||
* Test Before Compare Options method with diff keys | ||
*/ | ||
public function testBeforeCompareOptionsDiffKeys() | ||
{ | ||
$options1 = ['associated_product_1' => 3]; | ||
$options2 = ['associated_product_34' => 2]; | ||
|
||
$res = $this->model->beforeCompareOptions($this->subjectMock, $options1, $options2); | ||
|
||
$this->assertEquals($options1, $res[0]); | ||
$this->assertEquals($options2, $res[1]); | ||
} | ||
|
||
/** | ||
* Return mock array with wishlist options | ||
* | ||
* @param int $initVal | ||
* @param int $resVal | ||
* @param int $prodId | ||
* @return array | ||
*/ | ||
private function getWishlistAssocOption($initVal, $resVal, $prodId) | ||
{ | ||
$items = []; | ||
|
||
$optionMock = $this->createPartialMock( | ||
\Magento\Wishlist\Model\Item\Option::class, | ||
[ | ||
'getValue', | ||
] | ||
); | ||
$optionMock->expects($this->at(0))->method('getValue')->willReturn($initVal); | ||
$optionMock->expects($this->at(1))->method('getValue')->willReturn($resVal); | ||
|
||
$items['associated_product_' . $prodId] = $optionMock; | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* Return mock array with product options | ||
* | ||
* @param int $initVal | ||
* @param int $prodId | ||
* @return array | ||
*/ | ||
private function getProductAssocOption($initVal, $prodId) | ||
{ | ||
$items = []; | ||
|
||
$optionMock = $this->createPartialMock( | ||
\Magento\Catalog\Model\Product\Configuration\Item\Option::class, | ||
[ | ||
'getValue', | ||
] | ||
); | ||
|
||
$optionMock->expects($this->once())->method('getValue')->willReturn($initVal); | ||
|
||
$items['associated_product_' . $prodId] = $optionMock; | ||
|
||
return $items; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\Wishlist\Model\Item"> | ||
<plugin name="groupedProductWishlistProcessor" type="Magento\GroupedProduct\Model\Wishlist\Product\Item" /> | ||
</type> | ||
</config> |