Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cart.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Model\Product\Exception as ProductException;
11 
16 {
20  protected $wishlistProvider;
21 
25  protected $quantityProcessor;
26 
30  protected $itemFactory;
31 
35  protected $cart;
36 
40  protected $cartHelper;
41 
45  private $optionFactory;
46 
50  protected $productHelper;
51 
55  protected $escaper;
56 
60  protected $helper;
61 
65  protected $formKeyValidator;
66 
81  public function __construct(
82  Action\Context $context,
84  \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor,
85  \Magento\Wishlist\Model\ItemFactory $itemFactory,
86  \Magento\Checkout\Model\Cart $cart,
87  \Magento\Wishlist\Model\Item\OptionFactory $optionFactory,
88  \Magento\Catalog\Helper\Product $productHelper,
89  \Magento\Framework\Escaper $escaper,
90  \Magento\Wishlist\Helper\Data $helper,
91  \Magento\Checkout\Helper\Cart $cartHelper,
92  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
93  ) {
94  $this->wishlistProvider = $wishlistProvider;
95  $this->quantityProcessor = $quantityProcessor;
96  $this->itemFactory = $itemFactory;
97  $this->cart = $cart;
98  $this->optionFactory = $optionFactory;
99  $this->productHelper = $productHelper;
100  $this->escaper = $escaper;
101  $this->helper = $helper;
102  $this->cartHelper = $cartHelper;
103  $this->formKeyValidator = $formKeyValidator;
104  parent::__construct($context);
105  }
106 
118  public function execute()
119  {
121  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
122  if (!$this->formKeyValidator->validate($this->getRequest())) {
123  return $resultRedirect->setPath('*/*/');
124  }
125 
126  $itemId = (int)$this->getRequest()->getParam('item');
127  /* @var $item \Magento\Wishlist\Model\Item */
128  $item = $this->itemFactory->create()->load($itemId);
129  if (!$item->getId()) {
130  $resultRedirect->setPath('*/*');
131  return $resultRedirect;
132  }
133  $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
134  if (!$wishlist) {
135  $resultRedirect->setPath('*/*');
136  return $resultRedirect;
137  }
138 
139  // Set qty
140  $qty = $this->getRequest()->getParam('qty');
141  $postQty = $this->getRequest()->getPostValue('qty');
142  if ($postQty !== null && $qty !== $postQty) {
143  $qty = $postQty;
144  }
145  if (is_array($qty)) {
146  if (isset($qty[$itemId])) {
147  $qty = $qty[$itemId];
148  } else {
149  $qty = 1;
150  }
151  }
152  $qty = $this->quantityProcessor->process($qty);
153  if ($qty) {
154  $item->setQty($qty);
155  }
156 
157  $redirectUrl = $this->_url->getUrl('*/*');
158  $configureUrl = $this->_url->getUrl(
159  '*/*/configure/',
160  [
161  'id' => $item->getId(),
162  'product_id' => $item->getProductId(),
163  ]
164  );
165 
166  try {
168  $options = $this->optionFactory->create()->getCollection()->addItemFilter([$itemId]);
169  $item->setOptions($options->getOptionsByItem($itemId));
170 
171  $buyRequest = $this->productHelper->addParamsToBuyRequest(
172  $this->getRequest()->getParams(),
173  ['current_config' => $item->getBuyRequest()]
174  );
175 
176  $item->mergeBuyRequest($buyRequest);
177  $item->addToCart($this->cart, true);
178  $this->cart->save()->getQuote()->collectTotals();
179  $wishlist->save();
180 
181  if (!$this->cart->getQuote()->getHasError()) {
182  $message = __(
183  'You added %1 to your shopping cart.',
184  $this->escaper->escapeHtml($item->getProduct()->getName())
185  );
186  $this->messageManager->addSuccess($message);
187  }
188 
189  if ($this->cartHelper->getShouldRedirectToCart()) {
190  $redirectUrl = $this->cartHelper->getCartUrl();
191  } else {
192  $refererUrl = $this->_redirect->getRefererUrl();
193  if ($refererUrl && $refererUrl != $configureUrl) {
194  $redirectUrl = $refererUrl;
195  }
196  }
197  } catch (ProductException $e) {
198  $this->messageManager->addError(__('This product(s) is out of stock.'));
199  } catch (\Magento\Framework\Exception\LocalizedException $e) {
200  $this->messageManager->addNotice($e->getMessage());
201  $redirectUrl = $configureUrl;
202  } catch (\Exception $e) {
203  $this->messageManager->addException($e, __('We can\'t add the item to the cart right now.'));
204  }
205 
206  $this->helper->calculate();
207 
208  if ($this->getRequest()->isAjax()) {
210  $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
211  $resultJson->setData(['backUrl' => $redirectUrl]);
212  return $resultJson;
213  }
214 
215  $resultRedirect->setUrl($redirectUrl);
216  return $resultRedirect;
217  }
218 }
__construct(Action\Context $context, \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider, \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor, \Magento\Wishlist\Model\ItemFactory $itemFactory, \Magento\Checkout\Model\Cart $cart, \Magento\Wishlist\Model\Item\OptionFactory $optionFactory, \Magento\Catalog\Helper\Product $productHelper, \Magento\Framework\Escaper $escaper, \Magento\Wishlist\Helper\Data $helper, \Magento\Checkout\Helper\Cart $cartHelper, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator)
Definition: Cart.php:81
__()
Definition: __.php:13
$message
$wishlist
Definition: wishlist.php:10
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest