Skip to content
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

#11209 Wishlist Add grouped product Error #26258

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions app/code/Magento/GroupedProduct/Model/Wishlist/Product/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
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
) {
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
*/
public function beforeCompareOptions(
WishlistItem $subject,
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

$options1,
$options2
) {
$diff = array_diff_key($options1, $options2);

if (!$diff) {
foreach ($options1 as $key => $val) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use something like

foreach(array_keys($options) as $key) { ...

To avoid unused variables declaration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if (preg_match('/associated_product_\d+/', $key)) {
unset($options1[$key]);
unset($options2[$key]);
}
}
}

return [$options1, $options2];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?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()
{
$superGroup = [
'super_group' => [
33 => "0",
34 => 3,
35 => "0"
]
];

$superGroupObj = new \Magento\Framework\DataObject($superGroup);

$this->productMock->expects($this->once())->method('getId')->willReturn(34);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, assign repetitive values (34 and others) to a separate variable. It will make the code easier to maintain in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

$this->productMock->expects($this->once())->method('getTypeId')
->willReturn(TypeGrouped::TYPE_CODE);
$this->productMock->expects($this->once())->method('getCustomOptions')
->willReturn(
$this->getProductAssocOption(2, 34)
);

$wishlistItemProductMock = $this->createPartialMock(
\Magento\Catalog\Model\Product::class,
[
'getId',
]
);
$wishlistItemProductMock->expects($this->once())->method('getId')->willReturn(34);

$this->subjectMock->expects($this->once())->method('getProduct')
->willReturn($wishlistItemProductMock);
$this->subjectMock->expects($this->once())->method('getOptionsByCode')
->willReturn(
$this->getWishlistAssocOption(3, 5, 34)
);
$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;
}
}
12 changes: 12 additions & 0 deletions app/code/Magento/GroupedProduct/etc/frontend/di.xml
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>