/
Remove.php
64 lines (59 loc) · 2.39 KB
/
Remove.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Controller\Product\Compare;
use Magento\Framework\Exception\NoSuchEntityException;
class Remove extends \Magento\Catalog\Controller\Product\Compare
{
/**
* Remove item from compare list
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$productId = (int)$this->getRequest()->getParam('product');
if ($productId) {
$storeId = $this->_storeManager->getStore()->getId();
try {
$product = $this->productRepository->getById($productId, false, $storeId);
} catch (NoSuchEntityException $e) {
$product = null;
}
if ($product) {
/** @var $item \Magento\Catalog\Model\Product\Compare\Item */
$item = $this->_compareItemFactory->create();
if ($this->_customerSession->isLoggedIn()) {
$item->setCustomerId($this->_customerSession->getCustomerId());
} elseif ($this->_customerId) {
$item->setCustomerId($this->_customerId);
} else {
$item->addVisitorId($this->_customerVisitor->getId());
}
$item->loadByProduct($product);
/** @var $helper \Magento\Catalog\Helper\Product\Compare */
$helper = $this->_objectManager->get('Magento\Catalog\Helper\Product\Compare');
if ($item->getId()) {
$item->delete();
$productName = $this->_objectManager->get('Magento\Framework\Escaper')
->escapeHtml($product->getName());
$this->messageManager->addSuccess(
__('You removed product %1 from the comparison list.', $productName)
);
$this->_eventManager->dispatch(
'catalog_product_compare_remove_product',
['product' => $item]
);
$helper->calculate();
}
}
}
if (!$this->getRequest()->getParam('isAjax', false)) {
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setRefererOrBaseUrl();
}
}
}