Configure.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\Framework\App\Action;
  8. use Magento\Framework\Exception\NotFoundException;
  9. use Magento\Framework\Controller\ResultFactory;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class Configure extends \Magento\Wishlist\Controller\AbstractIndex
  14. {
  15. /**
  16. * Core registry
  17. *
  18. * @var \Magento\Framework\Registry
  19. */
  20. protected $_coreRegistry;
  21. /**
  22. * @var \Magento\Wishlist\Controller\WishlistProviderInterface
  23. */
  24. protected $wishlistProvider;
  25. /**
  26. * @var \Magento\Framework\View\Result\PageFactory
  27. */
  28. protected $resultPageFactory;
  29. /**
  30. * @param Action\Context $context
  31. * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
  32. * @param \Magento\Framework\Registry $coreRegistry
  33. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  34. */
  35. public function __construct(
  36. Action\Context $context,
  37. \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
  38. \Magento\Framework\Registry $coreRegistry,
  39. \Magento\Framework\View\Result\PageFactory $resultPageFactory
  40. ) {
  41. $this->wishlistProvider = $wishlistProvider;
  42. $this->_coreRegistry = $coreRegistry;
  43. $this->resultPageFactory = $resultPageFactory;
  44. parent::__construct($context);
  45. }
  46. /**
  47. * Action to reconfigure wishlist item
  48. *
  49. * @return \Magento\Framework\Controller\ResultInterface
  50. * @throws NotFoundException
  51. */
  52. public function execute()
  53. {
  54. $id = (int)$this->getRequest()->getParam('id');
  55. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  56. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  57. try {
  58. /* @var $item \Magento\Wishlist\Model\Item */
  59. $item = $this->_objectManager->create(\Magento\Wishlist\Model\Item::class);
  60. $item->loadWithOptions($id);
  61. if (!$item->getId()) {
  62. throw new \Magento\Framework\Exception\LocalizedException(
  63. __("The Wish List item can't load at this time. Please try again later.")
  64. );
  65. }
  66. $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
  67. if (!$wishlist) {
  68. throw new NotFoundException(__('Page not found.'));
  69. }
  70. $this->_coreRegistry->register('wishlist_item', $item);
  71. $params = new \Magento\Framework\DataObject();
  72. $params->setCategoryId(false);
  73. $params->setConfigureMode(true);
  74. $buyRequest = $item->getBuyRequest();
  75. if (!$buyRequest->getQty() && $item->getQty()) {
  76. $buyRequest->setQty($item->getQty());
  77. }
  78. if ($buyRequest->getQty() && !$item->getQty()) {
  79. $item->setQty($buyRequest->getQty());
  80. $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
  81. }
  82. $params->setBuyRequest($buyRequest);
  83. /** @var \Magento\Framework\View\Result\Page $resultPage */
  84. $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
  85. $this->_objectManager->get(
  86. \Magento\Catalog\Helper\Product\View::class
  87. )->prepareAndRender(
  88. $resultPage,
  89. $item->getProductId(),
  90. $this,
  91. $params
  92. );
  93. return $resultPage;
  94. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  95. $this->messageManager->addError($e->getMessage());
  96. $resultRedirect->setPath('*');
  97. return $resultRedirect;
  98. } catch (\Exception $e) {
  99. $this->messageManager->addError(__('We can\'t configure the product right now.'));
  100. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  101. $resultRedirect->setPath('*');
  102. return $resultRedirect;
  103. }
  104. }
  105. }