Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartUpdateBefore.php
Go to the documentation of this file.
1 <?php
8 
13 use Magento\Wishlist\Model\WishlistFactory;
14 
20 {
26  protected $wishlistData;
27 
31  protected $wishlistFactory;
32 
37  public function __construct(
39  WishlistFactory $wishlistFactory
40  ) {
41  $this->wishlistData = $wishlistData;
42  $this->wishlistFactory = $wishlistFactory;
43  }
44 
51  protected function getWishlist($customerId)
52  {
53  if (!$customerId) {
54  return false;
55  }
56  return $this->wishlistFactory->create()->loadByCustomerId($customerId, true);
57  }
58 
65  public function execute(Observer $observer)
66  {
67  $cart = $observer->getEvent()->getCart();
68  $data = $observer->getEvent()->getInfo()->toArray();
69  $productIds = [];
70 
71  $wishlist = $this->getWishlist($cart->getQuote()->getCustomerId());
72  if (!$wishlist) {
73  return $this;
74  }
75 
79  foreach ($data as $itemId => $itemInfo) {
80  if (!empty($itemInfo['wishlist']) && ($item = $cart->getQuote()->getItemById($itemId))) {
81  $productId = $item->getProductId();
82  $buyRequest = $item->getBuyRequest();
83 
84  if (array_key_exists('qty', $itemInfo) && is_numeric($itemInfo['qty'])) {
85  $buyRequest->setQty($itemInfo['qty']);
86  }
87  $wishlist->addNewItem($productId, $buyRequest);
88 
90  $cart->getQuote()->removeItem($itemId);
91  }
92  }
93 
94  if (count($productIds)) {
95  $wishlist->save();
96  $this->wishlistData->calculate();
97  }
98  return $this;
99  }
100 }
$wishlist
Definition: wishlist.php:10
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest
__construct(Data $wishlistData, WishlistFactory $wishlistFactory)