Configure.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Checkout\Controller\Cart;
  8. use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
  9. use Magento\Framework;
  10. use Magento\Framework\Controller\ResultFactory;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class Configure extends \Magento\Checkout\Controller\Cart implements HttpGetActionInterface
  15. {
  16. /**
  17. * @var \Magento\Framework\View\Result\PageFactory
  18. */
  19. protected $resultPageFactory;
  20. /**
  21. * @param \Magento\Framework\App\Action\Context $context
  22. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  23. * @param \Magento\Checkout\Model\Session $checkoutSession
  24. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  25. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  26. * @param \Magento\Checkout\Model\Cart $cart
  27. * @codeCoverageIgnore
  28. */
  29. public function __construct(
  30. Framework\App\Action\Context $context,
  31. Framework\App\Config\ScopeConfigInterface $scopeConfig,
  32. \Magento\Checkout\Model\Session $checkoutSession,
  33. \Magento\Store\Model\StoreManagerInterface $storeManager,
  34. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  35. \Magento\Checkout\Model\Cart $cart
  36. ) {
  37. parent::__construct(
  38. $context,
  39. $scopeConfig,
  40. $checkoutSession,
  41. $storeManager,
  42. $formKeyValidator,
  43. $cart
  44. );
  45. }
  46. /**
  47. * Action to reconfigure cart item
  48. *
  49. * @return \Magento\Framework\View\Result\Page|\Magento\Framework\Controller\Result\Redirect
  50. */
  51. public function execute()
  52. {
  53. // Extract item and product to configure
  54. $id = (int)$this->getRequest()->getParam('id');
  55. $productId = (int)$this->getRequest()->getParam('product_id');
  56. $quoteItem = null;
  57. if ($id) {
  58. $quoteItem = $this->cart->getQuote()->getItemById($id);
  59. }
  60. try {
  61. if (!$quoteItem || $productId != $quoteItem->getProduct()->getId()) {
  62. $this->messageManager->addErrorMessage(
  63. __("The quote item isn't found. Verify the item and try again.")
  64. );
  65. return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('checkout/cart');
  66. }
  67. $params = new \Magento\Framework\DataObject();
  68. $params->setCategoryId(false);
  69. $params->setConfigureMode(true);
  70. $params->setBuyRequest($quoteItem->getBuyRequest());
  71. $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
  72. $this->_objectManager->get(\Magento\Catalog\Helper\Product\View::class)
  73. ->prepareAndRender(
  74. $resultPage,
  75. $quoteItem->getProduct()->getId(),
  76. $this,
  77. $params
  78. );
  79. return $resultPage;
  80. } catch (\Exception $e) {
  81. $this->messageManager->addErrorMessage(__('We cannot configure the product.'));
  82. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  83. return $this->_goBack();
  84. }
  85. }
  86. }