Reorder.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Controller\AbstractController;
  7. use Magento\Framework\App\Action;
  8. use Magento\Framework\Registry;
  9. use Magento\Framework\App\Action\HttpPostActionInterface;
  10. /**
  11. * Abstract class for controllers Reorder(Customer) and Reorder(Guest)
  12. *
  13. * @package Magento\Sales\Controller\AbstractController
  14. */
  15. abstract class Reorder extends Action\Action implements HttpPostActionInterface
  16. {
  17. /**
  18. * @var \Magento\Sales\Controller\AbstractController\OrderLoaderInterface
  19. */
  20. protected $orderLoader;
  21. /**
  22. * @var Registry
  23. */
  24. protected $_coreRegistry;
  25. /**
  26. * @param Action\Context $context
  27. * @param OrderLoaderInterface $orderLoader
  28. * @param Registry $registry
  29. */
  30. public function __construct(
  31. Action\Context $context,
  32. OrderLoaderInterface $orderLoader,
  33. Registry $registry
  34. ) {
  35. $this->orderLoader = $orderLoader;
  36. $this->_coreRegistry = $registry;
  37. parent::__construct($context);
  38. }
  39. /**
  40. * Action for reorder
  41. *
  42. * @return \Magento\Framework\Controller\ResultInterface
  43. */
  44. public function execute()
  45. {
  46. $result = $this->orderLoader->load($this->_request);
  47. if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
  48. return $result;
  49. }
  50. $order = $this->_coreRegistry->registry('current_order');
  51. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  52. $resultRedirect = $this->resultRedirectFactory->create();
  53. /* @var $cart \Magento\Checkout\Model\Cart */
  54. $cart = $this->_objectManager->get(\Magento\Checkout\Model\Cart::class);
  55. $items = $order->getItemsCollection();
  56. foreach ($items as $item) {
  57. try {
  58. $cart->addOrderItem($item);
  59. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  60. if ($this->_objectManager->get(\Magento\Checkout\Model\Session::class)->getUseNotice(true)) {
  61. $this->messageManager->addNoticeMessage($e->getMessage());
  62. } else {
  63. $this->messageManager->addErrorMessage($e->getMessage());
  64. }
  65. return $resultRedirect->setPath('*/*/history');
  66. } catch (\Exception $e) {
  67. $this->messageManager->addExceptionMessage(
  68. $e,
  69. __('We can\'t add this item to your shopping cart right now.')
  70. );
  71. return $resultRedirect->setPath('checkout/cart');
  72. }
  73. }
  74. $cart->save();
  75. return $resultRedirect->setPath('checkout/cart');
  76. }
  77. }