UpdateItemOptions.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Controller\Index;
  7. use Magento\Catalog\Api\ProductRepositoryInterface;
  8. use Magento\Customer\Model\Session;
  9. use Magento\Framework\App\Action;
  10. use Magento\Framework\Data\Form\FormKey\Validator;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Framework\Controller\ResultFactory;
  13. use Magento\Wishlist\Controller\WishlistProviderInterface;
  14. /**
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class UpdateItemOptions extends \Magento\Wishlist\Controller\AbstractIndex
  18. {
  19. /**
  20. * @var WishlistProviderInterface
  21. */
  22. protected $wishlistProvider;
  23. /**
  24. * @var Session
  25. */
  26. protected $_customerSession;
  27. /**
  28. * @var ProductRepositoryInterface
  29. */
  30. protected $productRepository;
  31. /**
  32. * @var Validator
  33. */
  34. protected $formKeyValidator;
  35. /**
  36. * @param Action\Context $context
  37. * @param Session $customerSession
  38. * @param WishlistProviderInterface $wishlistProvider
  39. * @param ProductRepositoryInterface $productRepository
  40. * @param Validator $formKeyValidator
  41. */
  42. public function __construct(
  43. Action\Context $context,
  44. Session $customerSession,
  45. WishlistProviderInterface $wishlistProvider,
  46. ProductRepositoryInterface $productRepository,
  47. Validator $formKeyValidator
  48. ) {
  49. $this->_customerSession = $customerSession;
  50. $this->wishlistProvider = $wishlistProvider;
  51. $this->productRepository = $productRepository;
  52. $this->formKeyValidator = $formKeyValidator;
  53. parent::__construct($context);
  54. }
  55. /**
  56. * Action to accept new configuration for a wishlist item
  57. *
  58. * @return \Magento\Framework\Controller\Result\Redirect
  59. */
  60. public function execute()
  61. {
  62. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  63. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  64. if (!$this->formKeyValidator->validate($this->getRequest())) {
  65. return $resultRedirect->setPath('*/*/');
  66. }
  67. $productId = (int)$this->getRequest()->getParam('product');
  68. if (!$productId) {
  69. $resultRedirect->setPath('*/');
  70. return $resultRedirect;
  71. }
  72. try {
  73. $product = $this->productRepository->getById($productId);
  74. } catch (NoSuchEntityException $e) {
  75. $product = null;
  76. }
  77. if (!$product || !$product->isVisibleInCatalog()) {
  78. $this->messageManager->addError(__('We can\'t specify a product.'));
  79. $resultRedirect->setPath('*/');
  80. return $resultRedirect;
  81. }
  82. try {
  83. $id = (int)$this->getRequest()->getParam('id');
  84. /* @var \Magento\Wishlist\Model\Item */
  85. $item = $this->_objectManager->create(\Magento\Wishlist\Model\Item::class);
  86. $item->load($id);
  87. $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
  88. if (!$wishlist) {
  89. $resultRedirect->setPath('*/');
  90. return $resultRedirect;
  91. }
  92. $buyRequest = new \Magento\Framework\DataObject($this->getRequest()->getParams());
  93. $wishlist->updateItem($id, $buyRequest)->save();
  94. $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
  95. $this->_eventManager->dispatch(
  96. 'wishlist_update_item',
  97. ['wishlist' => $wishlist, 'product' => $product, 'item' => $wishlist->getItem($id)]
  98. );
  99. $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
  100. $message = __('%1 has been updated in your Wish List.', $product->getName());
  101. $this->messageManager->addSuccess($message);
  102. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  103. $this->messageManager->addError($e->getMessage());
  104. } catch (\Exception $e) {
  105. $this->messageManager->addError(__('We can\'t update your Wish List right now.'));
  106. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  107. }
  108. $resultRedirect->setPath('*/*', ['wishlist_id' => $wishlist->getId()]);
  109. return $resultRedirect;
  110. }
  111. }