-
Notifications
You must be signed in to change notification settings - Fork 9.4k
#11209 Wishlist Add grouped product Error #26258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 3 commits into
magento:2.4-develop
from
MaxRomanov4669:11209-wishlist-add-grouped-product-error
Jan 17, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may use
@SuppressWarnings(PHPMD.UnusedFormalParameter)
in case of this method if the$subject
variable is unused.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed