Skip to content

Commit 2e48ca1

Browse files
committed
luma compatibility remove compare list
1 parent dd5b443 commit 2e48ca1

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

app/code/Magento/Catalog/Controller/Product/Compare/Clear.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function execute()
3131

3232
try {
3333
$items->clear();
34+
$items->removeCompareList($this->_customerSession->getCustomerId());
3435
$this->messageManager->addSuccessMessage(__('You cleared the comparison list.'));
3536
$this->_objectManager->get(\Magento\Catalog\Helper\Product\Compare::class)->calculate();
3637
} catch (\Magento\Framework\Exception\LocalizedException $e) {

app/code/Magento/Catalog/Controller/Product/Compare/Remove.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Catalog\Controller\Product\Compare;
88

99
use Magento\Catalog\Model\Product\Attribute\Source\Status;
10+
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection;
1011
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213

@@ -49,6 +50,9 @@ public function execute()
4950
$helper = $this->_objectManager->get(\Magento\Catalog\Helper\Product\Compare::class);
5051
if ($item->getId()) {
5152
$item->delete();
53+
/** @var Collection $items */
54+
$items = $this->_itemCollectionFactory->create();
55+
$items->removeCompareList($this->_customerSession->getCustomerId());
5256
$productName = $this->_objectManager->get(\Magento\Framework\Escaper::class)
5357
->escapeHtml($product->getName());
5458
$this->messageManager->addSuccessMessage(

app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ public function getProductsByListId(int $listId): array
287287
return $this->getConnection()->fetchCol($select);
288288
}
289289

290+
290291
/**
291292
* Set list_id for customer compare item
292293
*
@@ -295,24 +296,53 @@ public function getProductsByListId(int $listId): array
295296
*/
296297
public function setListIdToCustomerCompareItems(int $listId, int $customerId)
297298
{
298-
$table = $this->getTable('catalog_compare_item');
299-
$select = $this->getConnection()->select()->
300-
from(
301-
$table
302-
)->where(
303-
'customer_id = ?',
304-
$customerId
305-
);
306-
$customerCompareItems = $this->getConnection()->fetchCol($select);
307-
foreach ($customerCompareItems as $itemId) {
299+
foreach ($this->getCustomerCompareItems($customerId) as $itemId) {
308300
$this->getConnection()->update(
309-
$table,
301+
$this->getTable('catalog_compare_item'),
310302
['list_id' => $listId],
311303
['catalog_compare_item_id = ?' => (int)$itemId]
312304
);
313305
}
314306
}
315307

308+
/**
309+
* Remove compare list if customer compare list empty
310+
*
311+
* @param int $customerId
312+
*/
313+
public function removeCompareList(int $customerId)
314+
{
315+
if (empty($this->getCustomerCompareItems($customerId))) {
316+
$this->getConnection()->delete(
317+
$this->getTable('catalog_compare_list'),
318+
['customer_id = ?' => $customerId]
319+
);
320+
}
321+
}
322+
323+
/**
324+
* Get customer compare items
325+
*
326+
* @param int|null $customerId
327+
* @return array
328+
*/
329+
private function getCustomerCompareItems(?int $customerId): array
330+
{
331+
if ($customerId) {
332+
$select = $this->getConnection()->select()->
333+
from(
334+
$this->getTable('catalog_compare_item')
335+
)->where(
336+
'customer_id = ?',
337+
$customerId
338+
);
339+
340+
return $this->getConnection()->fetchCol($select);
341+
}
342+
343+
return [];
344+
}
345+
316346
/**
317347
* Retrieve comapre products attribute set ids
318348
*

0 commit comments

Comments
 (0)