Skip to content

Commit 5b77eb8

Browse files
committed
MC-20139: Wishlist Items of customers not displaying on admin for secondary store
1 parent 4808977 commit 5b77eb8

File tree

4 files changed

+171
-35
lines changed

4 files changed

+171
-35
lines changed

app/code/Magento/Wishlist/Model/ResourceModel/Item/Collection/Grid.php

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66

77
namespace Magento\Wishlist\Model\ResourceModel\Item\Collection;
88

9-
use Magento\Customer\Controller\RegistryConstants as RegistryConstants;
9+
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
10+
use Magento\Customer\Controller\RegistryConstants;
11+
use Magento\Wishlist\Model\Item;
1012

1113
/**
12-
* Wishlist item collection grouped by customer id
14+
* Wishlist item collection for grid grouped by customer id
1315
*
1416
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1517
*/
1618
class Grid extends \Magento\Wishlist\Model\ResourceModel\Item\Collection
1719
{
20+
/**
21+
* Load product attributes to present in grid
22+
*/
23+
private const PRODUCT_ATTRIBUTES_TO_GRID = [
24+
'name',
25+
'price',
26+
];
27+
1828
/**
1929
* @var \Magento\Framework\Registry
2030
*/
@@ -88,30 +98,48 @@ public function __construct(
8898
}
8999

90100
/**
91-
* Initialize db select
92-
*
93-
* @return $this
101+
* @inheritdoc
94102
*/
95103
protected function _initSelect()
96104
{
97105
parent::_initSelect();
98-
$this->addCustomerIdFilter(
99-
$this->_registryManager->registry(RegistryConstants::CURRENT_CUSTOMER_ID)
100-
)
101-
->resetSortOrder()
102-
->addDaysInWishlist()
106+
107+
$customerId = $this->_registryManager->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
108+
$this->addDaysInWishlist()
103109
->addStoreData()
104-
->setVisibilityFilter()
105-
->setInStockFilter();
110+
->addCustomerIdFilter($customerId)
111+
->resetSortOrder();
112+
106113
return $this;
107114
}
108115

109116
/**
110-
* Add select order
111-
*
112-
* @param string $field
113-
* @param string $direction
114-
* @return \Magento\Framework\Data\Collection\AbstractDb
117+
* @inheritdoc
118+
*/
119+
protected function _assignProducts()
120+
{
121+
/** @var ProductCollection $productCollection */
122+
$productCollection = $this->_productCollectionFactory->create()
123+
->addAttributeToSelect(self::PRODUCT_ATTRIBUTES_TO_GRID)
124+
->addIdFilter($this->_productIds);
125+
126+
/** @var Item $item */
127+
foreach ($this as $item) {
128+
$product = $productCollection->getItemById($item->getProductId());
129+
if ($product) {
130+
$product->setCustomOptions([]);
131+
$item->setProduct($product);
132+
$item->setProductName($product->getName());
133+
$item->setName($product->getName());
134+
$item->setPrice($product->getPrice());
135+
}
136+
}
137+
138+
return $this;
139+
}
140+
141+
/**
142+
* @inheritdoc
115143
*/
116144
public function setOrder($field, $direction = self::SORT_ORDER_DESC)
117145
{
@@ -127,24 +155,7 @@ public function setOrder($field, $direction = self::SORT_ORDER_DESC)
127155
}
128156

129157
/**
130-
* Add quantity to filter
131-
*
132-
* @param string $field
133-
* @param array $condition
134-
* @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
135-
*/
136-
private function addQtyFilter(string $field, array $condition)
137-
{
138-
return parent::addFieldToFilter('main_table.' . $field, $condition);
139-
}
140-
141-
/**
142-
* Add field filter to collection
143-
*
144-
* @param string|array $field
145-
* @param null|string|array $condition
146-
* @see self::_getConditionSql for $condition
147-
* @return \Magento\Framework\Data\Collection\AbstractDb
158+
* @inheritdoc
148159
*/
149160
public function addFieldToFilter($field, $condition = null)
150161
{
@@ -168,6 +179,19 @@ public function addFieldToFilter($field, $condition = null)
168179
return $this->addQtyFilter($field, $condition);
169180
}
170181
}
182+
171183
return parent::addFieldToFilter($field, $condition);
172184
}
185+
186+
/**
187+
* Add quantity to filter
188+
*
189+
* @param string $field
190+
* @param array $condition
191+
* @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
192+
*/
193+
private function addQtyFilter(string $field, array $condition)
194+
{
195+
return parent::addFieldToFilter('main_table.' . $field, $condition);
196+
}
173197
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\ResourceModel\Item\Collection;
9+
10+
use Magento\Customer\Controller\RegistryConstants;
11+
use Magento\Customer\Model\Customer;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Registry;
14+
use Magento\Store\Model\Website;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Class to test wishlist collection by customer functionality
19+
*
20+
* @magentoAppArea adminhtml
21+
*/
22+
class GridTest extends TestCase
23+
{
24+
/**
25+
* @var ObjectManager
26+
*/
27+
private $objectManager;
28+
29+
/**
30+
* @var Registry
31+
*/
32+
private $registryManager;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function setUp()
38+
{
39+
$this->objectManager = ObjectManager::getInstance();
40+
$this->registryManager = $this->objectManager->get(Registry::class);
41+
}
42+
43+
/**
44+
* Test to load wishlist collection by customer on second website
45+
*
46+
* @magentoDbIsolation disabled
47+
* @magentoDataFixture Magento/Wishlist/_files/wishlist_on_second_website.php
48+
*/
49+
public function testLoadOnSecondWebsite()
50+
{
51+
$customer = $this->loadCustomer();
52+
$this->registryManager->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customer->getId());
53+
54+
$gridCollection = $this->objectManager->get(Grid::class);
55+
$this->assertNotEmpty($gridCollection->getItems());
56+
}
57+
58+
/**
59+
* Load customer in second website
60+
*
61+
* @return Customer
62+
*/
63+
private function loadCustomer(): Customer
64+
{
65+
/** @var $website Website */
66+
$website = $this->objectManager->get(Website::class);
67+
$website->load('newwebsite', 'code');
68+
69+
/** @var Customer $customer */
70+
$customer = $this->objectManager->get(Customer::class);
71+
$customer->setWebsiteId($website->getId());
72+
$customer->loadByEmail('customer2@example.com');
73+
74+
return $customer;
75+
}
76+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
require __DIR__ . '/../../../Magento/Catalog/_files/products_with_websites_and_stores.php';
9+
require __DIR__ . '/../../../Magento/Customer/_files/customer_non_default_website_id.php';
10+
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\Wishlist\Model\Wishlist;
14+
15+
$objectManager = Bootstrap::getObjectManager();
16+
17+
/** @var ProductRepositoryInterface $productRepository */
18+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
19+
$simpleProduct = $productRepository->get('simple-2');
20+
21+
/* @var $wishlist Wishlist */
22+
$wishlist = Bootstrap::getObjectManager()->create(Wishlist::class);
23+
$wishlist->loadByCustomerId($customer->getId(), true);
24+
$wishlist->addNewItem($simpleProduct);
25+
$wishlist->setSharingCode('fixture_unique_code')
26+
->setShared(1)
27+
->save();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
require __DIR__ . '/../../../Magento/Catalog/_files/products_with_websites_and_stores_rollback.php';
9+
require __DIR__ . '/../../../Magento/Customer/_files/customer_non_default_website_id_rollback.php';

0 commit comments

Comments
 (0)