Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Price.php
Go to the documentation of this file.
1 <?php
8 
15 
17 {
21  protected $productRepository;
22 
28  public function __construct(
29  Context $context,
32  ) {
33  $this->productRepository = $productRepository;
34  parent::__construct($context, $customerSession);
35  }
36 
40  public function execute()
41  {
42  $productId = (int)$this->getRequest()->getParam('product');
44  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
45  if (!$productId) {
46  $resultRedirect->setPath('/');
47  return $resultRedirect;
48  }
49 
50  try {
51  /* @var $product \Magento\Catalog\Model\Product */
52  $product = $this->productRepository->getById($productId);
53  if (!$product->isVisibleInCatalog()) {
54  throw new NoSuchEntityException();
55  }
57  $model = $this->_objectManager->create(\Magento\ProductAlert\Model\Price::class)
58  ->setCustomerId($this->customerSession->getCustomerId())
59  ->setProductId($product->getId())
60  ->setWebsiteId(
61  $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
62  ->getStore()
63  ->getWebsiteId()
64  )
65  ->loadByParam();
66  if ($model->getId()) {
67  $model->delete();
68  }
69 
70  $this->messageManager->addSuccess(__('You deleted the alert subscription.'));
71  } catch (NoSuchEntityException $noEntityException) {
72  $this->messageManager->addError(__("The product wasn't found. Verify the product and try again."));
73  $resultRedirect->setPath('customer/account/');
74  return $resultRedirect;
75  } catch (\Exception $e) {
76  $this->messageManager->addException(
77  $e,
78  __("The alert subscription couldn't update at this time. Please try again later.")
79  );
80  }
81  $resultRedirect->setUrl($product->getProductUrl());
82  return $resultRedirect;
83  }
84 }
__()
Definition: __.php:13
__construct(Context $context, CustomerSession $customerSession, ProductRepositoryInterface $productRepository)
Definition: Price.php:28