Skip to content

Commit 2da9d00

Browse files
committed
MAGETWO-91433: ProductListing: Grid view is getting changed to List view when user adding product from wishlist section.
1 parent 59e10c6 commit 2da9d00

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

app/code/Magento/Wishlist/Controller/Index/Remove.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66
namespace Magento\Wishlist\Controller\Index;
77

8-
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
98
use Magento\Framework\App\Action;
109
use Magento\Framework\Data\Form\FormKey\Validator;
1110
use Magento\Framework\Exception\NotFoundException;
1211
use Magento\Framework\Controller\ResultFactory;
1312
use Magento\Wishlist\Controller\WishlistProviderInterface;
1413
use Magento\Wishlist\Model\Item;
14+
use Magento\Wishlist\Model\Product\AttributeValueProvider;
1515

1616
/**
1717
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -29,26 +29,26 @@ class Remove extends \Magento\Wishlist\Controller\AbstractIndex
2929
protected $formKeyValidator;
3030

3131
/**
32-
* @var ProductCollectionFactory
32+
* @var AttributeValueProvider
3333
*/
34-
private $productCollectionFactory;
34+
private $attributeValueProvider;
3535

3636
/**
3737
* @param Action\Context $context
3838
* @param WishlistProviderInterface $wishlistProvider
3939
* @param Validator $formKeyValidator
40-
* @param ProductCollectionFactory|null $productCollectionFactory
40+
* @param AttributeValueProvider|null $attributeValueProvider
4141
*/
4242
public function __construct(
4343
Action\Context $context,
4444
WishlistProviderInterface $wishlistProvider,
4545
Validator $formKeyValidator,
46-
ProductCollectionFactory $productCollectionFactory = null
46+
AttributeValueProvider $attributeValueProvider = null
4747
) {
4848
$this->wishlistProvider = $wishlistProvider;
4949
$this->formKeyValidator = $formKeyValidator;
50-
$this->productCollectionFactory = $productCollectionFactory
51-
?: \Magento\Framework\App\ObjectManager::getInstance()->get(ProductCollectionFactory::class);
50+
$this->attributeValueProvider = $attributeValueProvider
51+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class);
5252
parent::__construct($context);
5353
}
5454

@@ -79,15 +79,12 @@ public function execute()
7979
try {
8080
$item->delete();
8181
$wishlist->save();
82-
$product = $this->productCollectionFactory
83-
->create()
84-
->addIdFilter($item->getProductId())
85-
->addAttributeToSelect('name')
86-
->getFirstItem();
82+
$productName = $this->attributeValueProvider
83+
->getRawAttributeValue($item->getProductId(), 'name');
8784
$this->messageManager->addComplexSuccessMessage(
8885
'removeWishlistItemSuccessMessage',
8986
[
90-
'product_name' => $product->getName()
87+
'product_name' => $productName,
9188
]
9289
);
9390
} catch (\Magento\Framework\Exception\LocalizedException $e) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Wishlist\Model\Product;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
13+
/**
14+
* Provides existing attribute value for a product entity.
15+
*/
16+
class AttributeValueProvider
17+
{
18+
/**
19+
* @var ProductCollectionFactory
20+
*/
21+
private $productCollectionFactory;
22+
23+
/**
24+
* @param ProductCollectionFactory $productCollectionFactory
25+
*/
26+
public function __construct(
27+
ProductCollectionFactory $productCollectionFactory
28+
) {
29+
$this->productCollectionFactory = $productCollectionFactory;
30+
}
31+
32+
/**
33+
* Provides existing raw attribute value by the attribute code of the product entity.
34+
*
35+
* @param int $productId
36+
* @param string $attributeCode
37+
* @param int|null $storeId
38+
* @return null|string
39+
* @throws NoSuchEntityException
40+
*/
41+
public function getRawAttributeValue(int $productId, string $attributeCode, int $storeId = null):? string
42+
{
43+
44+
$collection = $this->productCollectionFactory->create();
45+
$collection->addIdFilter($productId)
46+
->addStoreFilter($storeId)
47+
->addAttributeToSelect($attributeCode);
48+
49+
$data = $collection->getConnection()->fetchRow($collection->getSelect());
50+
51+
if (!array_key_exists($attributeCode, $data)) {
52+
throw new NoSuchEntityException(__('An attribute value of "%1" does not exist.', $attributeCode));
53+
}
54+
55+
return $data[$attributeCode];
56+
}
57+
}

0 commit comments

Comments
 (0)