Addgroup.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Checkout\Model\Cart as CustomerCart;
  9. use Magento\Framework\Escaper;
  10. use Magento\Framework\App\ObjectManager;
  11. use Magento\Sales\Model\Order\Item;
  12. /**
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class Addgroup extends \Magento\Checkout\Controller\Cart
  16. {
  17. /**
  18. * @var Escaper
  19. */
  20. private $escaper;
  21. /**
  22. * @param \Magento\Framework\App\Action\Context $context
  23. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  24. * @param \Magento\Checkout\Model\Session $checkoutSession
  25. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  26. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  27. * @param CustomerCart $cart
  28. * @param Escaper|null $escaper
  29. */
  30. public function __construct(
  31. \Magento\Framework\App\Action\Context $context,
  32. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  33. \Magento\Checkout\Model\Session $checkoutSession,
  34. \Magento\Store\Model\StoreManagerInterface $storeManager,
  35. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  36. CustomerCart $cart,
  37. Escaper $escaper = null
  38. ) {
  39. $this->escaper = $escaper ?: ObjectManager::getInstance()->get(\Magento\Framework\Escaper::class);
  40. parent::__construct($context, $scopeConfig, $checkoutSession, $storeManager, $formKeyValidator, $cart);
  41. }
  42. /**
  43. * @return \Magento\Framework\Controller\Result\Redirect
  44. */
  45. public function execute()
  46. {
  47. $orderItemIds = $this->getRequest()->getPost('order_items');
  48. if (is_array($orderItemIds)) {
  49. $itemsCollection = $this->_objectManager->create(\Magento\Sales\Model\Order\Item::class)
  50. ->getCollection()
  51. ->addIdFilter($orderItemIds)
  52. ->load();
  53. /* @var $itemsCollection \Magento\Sales\Model\ResourceModel\Order\Item\Collection */
  54. foreach ($itemsCollection as $item) {
  55. try {
  56. $this->addOrderItem($item);
  57. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  58. if ($this->_checkoutSession->getUseNotice(true)) {
  59. $this->messageManager->addNoticeMessage($e->getMessage());
  60. } else {
  61. $this->messageManager->addErrorMessage($e->getMessage());
  62. }
  63. } catch (\Exception $e) {
  64. $this->messageManager->addExceptionMessage(
  65. $e,
  66. __('We can\'t add this item to your shopping cart right now.')
  67. );
  68. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  69. return $this->_goBack();
  70. }
  71. }
  72. $this->cart->save();
  73. }
  74. return $this->_goBack();
  75. }
  76. /**
  77. * Add item to cart.
  78. *
  79. * Add item to cart only if it's belongs to customer.
  80. *
  81. * @param Item $item
  82. * @return void
  83. */
  84. private function addOrderItem(Item $item)
  85. {
  86. /** @var \Magento\Customer\Model\Session $session */
  87. $session = $this->cart->getCustomerSession();
  88. if ($session->isLoggedIn()) {
  89. $orderCustomerId = $item->getOrder()->getCustomerId();
  90. $currentCustomerId = $session->getCustomer()->getId();
  91. if ($orderCustomerId == $currentCustomerId) {
  92. $this->cart->addOrderItem($item, 1);
  93. if (!$this->cart->getQuote()->getHasError()) {
  94. $message = __(
  95. 'You added %1 to your shopping cart.',
  96. $this->escaper->escapeHtml($item->getName())
  97. );
  98. $this->messageManager->addSuccessMessage($message);
  99. }
  100. }
  101. }
  102. }
  103. }