Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ItemCarrier.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Wishlist\Model;
8 
9 use Magento\Catalog\Model\Product\Exception as ProductException;
10 use Magento\Checkout\Helper\Cart as CartHelper;
15 use Psr\Log\LoggerInterface as Logger;
16 use Magento\Framework\Message\ManagerInterface as MessageManager;
18 use Magento\Wishlist\Helper\Data as WishlistHelper;
19 
24 {
28  protected $customerSession;
29 
33  protected $quantityProcessor;
34 
38  protected $cart;
39 
43  protected $logger;
44 
48  protected $helper;
49 
53  protected $cartHelper;
54 
58  protected $urlBuilder;
59 
63  protected $messageManager;
64 
68  protected $redirector;
69 
81  public function __construct(
84  Cart $cart,
85  Logger $logger,
86  WishlistHelper $helper,
87  CartHelper $cartHelper,
89  MessageManager $messageManager,
91  ) {
92  $this->customerSession = $customerSession;
93  $this->quantityProcessor = $quantityProcessor;
94  $this->cart = $cart;
95  $this->logger = $logger;
96  $this->helper = $helper;
97  $this->cartHelper = $cartHelper;
98  $this->urlBuilder = $urlBuilder;
99  $this->messageManager = $messageManager;
100  $this->redirector = $redirector;
101  }
102 
113  public function moveAllToCart(Wishlist $wishlist, $qtys)
114  {
115  $isOwner = $wishlist->isOwner($this->customerSession->getCustomerId());
116 
117  $messages = [];
118  $addedProducts = [];
119  $notSalable = [];
120 
121  $cart = $this->cart;
122  $collection = $wishlist->getItemCollection()->setVisibilityFilter();
123 
124  foreach ($collection as $item) {
126  try {
127  $disableAddToCart = $item->getProduct()->getDisableAddToCart();
128  $item->unsProduct();
129 
130  // Set qty
131  if (isset($qtys[$item->getId()])) {
132  $qty = $this->quantityProcessor->process($qtys[$item->getId()]);
133  if ($qty) {
134  $item->setQty($qty);
135  }
136  }
137  $item->getProduct()->setDisableAddToCart($disableAddToCart);
138  // Add to cart
139  if ($item->addToCart($cart, $isOwner)) {
140  $addedProducts[] = $item->getProduct();
141  }
142  } catch (LocalizedException $e) {
143  if ($e instanceof ProductException) {
144  $notSalable[] = $item;
145  } else {
146  $messages[] = __('%1 for "%2".', trim($e->getMessage(), '.'), $item->getProduct()->getName());
147  }
148 
149  $cartItem = $cart->getQuote()->getItemByProduct($item->getProduct());
150  if ($cartItem) {
151  $cart->getQuote()->deleteItem($cartItem);
152  }
153  } catch (\Exception $e) {
154  $this->logger->critical($e);
155  $messages[] = __('We can\'t add this item to your shopping cart right now.');
156  }
157  }
158 
159  if ($isOwner) {
160  $indexUrl = $this->helper->getListUrl($wishlist->getId());
161  } else {
162  $indexUrl = $this->urlBuilder->getUrl('wishlist/shared', ['code' => $wishlist->getSharingCode()]);
163  }
164  if ($this->cartHelper->getShouldRedirectToCart()) {
165  $redirectUrl = $this->cartHelper->getCartUrl();
166  } elseif ($this->redirector->getRefererUrl()) {
167  $redirectUrl = $this->redirector->getRefererUrl();
168  } else {
169  $redirectUrl = $indexUrl;
170  }
171 
172  if ($notSalable) {
173  $products = [];
174  foreach ($notSalable as $item) {
175  $products[] = '"' . $item->getProduct()->getName() . '"';
176  }
177  $messages[] = __(
178  'We couldn\'t add the following product(s) to the shopping cart: %1.',
179  join(', ', $products)
180  );
181  }
182 
183  if ($messages) {
184  foreach ($messages as $message) {
185  $this->messageManager->addError($message);
186  }
187  $redirectUrl = $indexUrl;
188  }
189 
190  if ($addedProducts) {
191  // save wishlist model for setting date of last update
192  try {
193  $wishlist->save();
194  } catch (\Exception $e) {
195  $this->messageManager->addError(__('We can\'t update the Wish List right now.'));
196  $redirectUrl = $indexUrl;
197  }
198 
199  $products = [];
200  foreach ($addedProducts as $product) {
202  $products[] = '"' . $product->getName() . '"';
203  }
204 
205  $this->messageManager->addSuccess(
206  __('%1 product(s) have been added to shopping cart: %2.', count($addedProducts), join(', ', $products))
207  );
208 
209  // save cart and collect totals
210  $cart->save()->getQuote()->collectTotals();
211  }
212  $this->helper->calculate();
213  return $redirectUrl;
214  }
215 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$message
$wishlist
Definition: wishlist.php:10
__construct(Session $customerSession, LocaleQuantityProcessor $quantityProcessor, Cart $cart, Logger $logger, WishlistHelper $helper, CartHelper $cartHelper, UrlInterface $urlBuilder, MessageManager $messageManager, RedirectInterface $redirector)
Definition: ItemCarrier.php:81