public function testExecute() { $event = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); /** @var $event \Magento\Framework\Event\Observer */ $this->helper->expects($this->once())->method('calculate'); $this->observer->execute($event); }
/** * Add cart item to wishlist and remove from cart * * @return \Magento\Framework\Controller\Result\Redirect * @throws NotFoundException * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function execute() { $wishlist = $this->wishlistProvider->getWishlist(); if (!$wishlist) { throw new NotFoundException(__('Page not found.')); } try { $itemId = (int) $this->getRequest()->getParam('item'); $item = $this->cart->getQuote()->getItemById($itemId); if (!$item) { throw new LocalizedException(__('The requested cart item doesn\'t exist.')); } $productId = $item->getProductId(); $buyRequest = $item->getBuyRequest(); $wishlist->addNewItem($productId, $buyRequest); $this->cart->getQuote()->removeItem($itemId); $this->cart->save(); $this->wishlistHelper->calculate(); $wishlist->save(); $this->messageManager->addSuccessMessage(__("%1 has been moved to your wish list.", $this->escaper->escapeHtml($item->getProduct()->getName()))); } catch (LocalizedException $e) { $this->messageManager->addErrorMessage($e->getMessage()); } catch (\Exception $e) { $this->messageManager->addExceptionMessage($e, __('We can\'t move the item to the wish list.')); } /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); return $resultRedirect->setUrl($this->cartHelper->getCartUrl()); }
/** * @return string */ protected function _toHtml() { if ($this->_wishlistHelper->isAllow()) { return parent::_toHtml(); } return ''; }
/** * Check move quote item to wishlist request * * @param Observer $observer * @return $this */ public function execute(Observer $observer) { $cart = $observer->getEvent()->getCart(); $data = $observer->getEvent()->getInfo()->toArray(); $productIds = []; $wishlist = $this->getWishlist($cart->getQuote()->getCustomerId()); if (!$wishlist) { return $this; } /** * Collect product ids marked for move to wishlist */ foreach ($data as $itemId => $itemInfo) { if (!empty($itemInfo['wishlist']) && ($item = $cart->getQuote()->getItemById($itemId))) { $productId = $item->getProductId(); $buyRequest = $item->getBuyRequest(); if (array_key_exists('qty', $itemInfo) && is_numeric($itemInfo['qty'])) { $buyRequest->setQty($itemInfo['qty']); } $wishlist->addNewItem($productId, $buyRequest); $productIds[] = $productId; $cart->getQuote()->removeItem($itemId); } } if (count($productIds)) { $wishlist->save(); $this->wishlistData->calculate(); } return $this; }
/** * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecute() { $customerId = 1; $itemId = 5; $itemQty = 123; $productId = 321; $eventObserver = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); $event = $this->getMockBuilder('Magento\\Framework\\Event')->setMethods(['getCart', 'getInfo'])->disableOriginalConstructor()->getMock(); $eventObserver->expects($this->exactly(2))->method('getEvent')->willReturn($event); $quoteItem = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item')->setMethods(['getProductId', 'getBuyRequest', '__wakeup'])->disableOriginalConstructor()->getMock(); $buyRequest = $this->getMockBuilder('Magento\\Framework\\DataObject')->setMethods(['setQty'])->disableOriginalConstructor()->getMock(); $infoData = $this->getMockBuilder('Magento\\Framework\\DataObject')->setMethods(['toArray'])->disableOriginalConstructor()->getMock(); $infoData->expects($this->once())->method('toArray')->willReturn([$itemId => ['qty' => $itemQty, 'wishlist' => true]]); $cart = $this->getMockBuilder('Magento\\Checkout\\Model\\Cart')->disableOriginalConstructor()->getMock(); $quote = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->setMethods(['getCustomerId', 'getItemById', 'removeItem', '__wakeup'])->disableOriginalConstructor()->getMock(); $event->expects($this->once())->method('getCart')->willReturn($cart); $event->expects($this->once())->method('getInfo')->willReturn($infoData); $cart->expects($this->any())->method('getQuote')->willReturn($quote); $quoteItem->expects($this->once())->method('getProductId')->willReturn($productId); $quoteItem->expects($this->once())->method('getBuyRequest')->willReturn($buyRequest); $buyRequest->expects($this->once())->method('setQty')->with($itemQty)->willReturnSelf(); $quote->expects($this->once())->method('getCustomerId')->willReturn($customerId); $quote->expects($this->once())->method('getItemById')->with($itemId)->willReturn($quoteItem); $quote->expects($this->once())->method('removeItem')->with($itemId); $this->wishlist->expects($this->once())->method('loadByCustomerId')->with($this->logicalOr($customerId, true))->willReturnSelf(); $this->wishlist->expects($this->once())->method('addNewItem')->with($this->logicalOr($productId, $buyRequest)); $this->wishlist->expects($this->once())->method('save'); $this->helper->expects($this->once())->method('calculate'); /** @var $eventObserver \Magento\Framework\Event\Observer */ $this->assertSame($this->observer, $this->observer->execute($eventObserver)); }
public function testGetMoveFromCartParams() { $itemId = 45; $json = '{json;}'; /** * @var Item|\PHPUnit_Framework_MockObject_MockObject $itemMock */ $itemMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item')->disableOriginalConstructor()->getMock(); $itemMock->expects($this->once())->method('getId')->willReturn($itemId); $this->wishlistHelperMock->expects($this->once())->method('getMoveFromCartParams')->with($itemId)->willReturn($json); $this->model->setItem($itemMock); $this->assertEquals($json, $this->model->getMoveFromCartParams()); }
/** * Get wishlist items * * @return array */ protected function getItems() { $this->view->loadLayout(); $collection = $this->wishlistHelper->getWishlistItemCollection(); $collection->clear()->setPageSize(self::SIDEBAR_ITEMS_NUMBER)->setInStockFilter(true)->setOrder('added_at'); $items = []; foreach ($collection as $wishlistItem) { /** @var \Magento\Catalog\Model\Product $product */ $product = $wishlistItem->getProduct(); $this->productImageView->init($product, 'wishlist_sidebar_block', 'Magento_Catalog'); $items[] = ['image' => ['src' => $this->productImageView->getUrl(), 'alt' => $this->productImageView->getLabel(), 'width' => $this->productImageView->getWidth(), 'height' => $this->productImageView->getHeight()], 'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem), 'product_name' => $product->getName(), 'product_price' => $this->block->getProductPriceHtml($product, \Magento\Catalog\Pricing\Price\ConfiguredPriceInterface::CONFIGURED_PRICE_CODE, \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST, ['item' => $wishlistItem]), 'product_is_saleable_and_visible' => $product->isSaleable() && $product->isVisibleInSiteVisibility(), 'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product), 'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem), 'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem)]; } return $items; }
/** * @return string */ protected function getLinkParams() { $params = []; $wishlistId = $this->wishlistHelper->getWishlist()->getId(); $customer = $this->wishlistHelper->getCustomer(); if ($customer) { $key = $customer->getId() . ',' . $customer->getEmail(); $params = ['type' => 'wishlist', 'data' => $this->urlEncoder->encode($key), '_secure' => false]; } if ($wishlistId) { $params['wishlist_id'] = $wishlistId; } return $params; }
public function testGetSharedAddAllToCartUrl() { $url = 'result url'; $this->store->expects($this->once())->method('getUrl')->with('*/*/allcart', ['_current' => true])->willReturn($url); $this->postDataHelper->expects($this->once())->method('getPostData')->with($url)->willReturn($url); $this->assertEquals($url, $this->model->getSharedAddAllToCartUrl()); }
/** * Add wishlist item to shopping cart and remove from wishlist * * If Product has required options - item removed from wishlist and redirect * to product view page with message about needed defined required options * * @return ResponseInterface * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute() { $itemId = (int) $this->getRequest()->getParam('item'); /* @var $item \Magento\Wishlist\Model\Item */ $item = $this->itemFactory->create()->load($itemId); if (!$item->getId()) { return $this->_redirect('*/*'); } $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId()); if (!$wishlist) { return $this->_redirect('*/*'); } // Set qty $qty = $this->getRequest()->getParam('qty'); if (is_array($qty)) { if (isset($qty[$itemId])) { $qty = $qty[$itemId]; } else { $qty = 1; } } $qty = $this->quantityProcessor->process($qty); if ($qty) { $item->setQty($qty); } $redirectUrl = $this->_url->getUrl('*/*'); $configureUrl = $this->_url->getUrl('*/*/configure/', ['id' => $item->getId(), 'product_id' => $item->getProductId()]); try { /** @var \Magento\Wishlist\Model\Resource\Item\Option\Collection $options */ $options = $this->optionFactory->create()->getCollection()->addItemFilter([$itemId]); $item->setOptions($options->getOptionsByItem($itemId)); $buyRequest = $this->productHelper->addParamsToBuyRequest($this->getRequest()->getParams(), ['current_config' => $item->getBuyRequest()]); $item->mergeBuyRequest($buyRequest); $item->addToCart($this->cart, true); $this->cart->save()->getQuote()->collectTotals(); $wishlist->save(); if (!$this->cart->getQuote()->getHasError()) { $message = __('You added %1 to your shopping cart.', $this->escaper->escapeHtml($item->getProduct()->getName())); $this->messageManager->addSuccess($message); } if ($this->cart->getShouldRedirectToCart()) { $redirectUrl = $this->cart->getCartUrl(); } else { $refererUrl = $this->_redirect->getRefererUrl(); if ($refererUrl && $refererUrl != $configureUrl) { $redirectUrl = $refererUrl; } } } catch (ProductException $e) { $this->messageManager->addError(__('This product(s) is out of stock.')); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addNotice($e->getMessage()); $redirectUrl = $configureUrl; } catch (\Exception $e) { $this->messageManager->addException($e, __('Cannot add item to shopping cart')); } $this->helper->calculate(); return $this->getResponse()->setRedirect($redirectUrl); }
/** * Retrieve wishlist name * * @return string */ public function getName() { $name = $this->_getData('name'); if (!strlen($name)) { return $this->_wishlistData->getDefaultWishlistName(); } return $name; }
/** * Configure product view blocks * * @return $this */ protected function _prepareLayout() { // Set custom add to cart url $block = $this->getLayout()->getBlock('product.info'); if ($block && $this->getWishlistItem()) { $url = $this->_wishlistData->getAddToCartUrl($this->getWishlistItem()); $block->setCustomAddToCartUrl($url); } return parent::_prepareLayout(); }
protected function setUp() { $wishlist = $this->getMock('Magento\\Wishlist\\Model\\Wishlist', ['getId', 'getSharingCode'], [], '', false); $wishlist->expects($this->any())->method('getId')->will($this->returnValue(5)); $wishlist->expects($this->any())->method('getSharingCode')->will($this->returnValue('somesharingcode')); $customer = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false); $customer->expects($this->any())->method('getId')->will($this->returnValue(8)); $customer->expects($this->any())->method('getEmail')->will($this->returnValue('*****@*****.**')); $this->wishlistHelper = $this->getMock('Magento\\Wishlist\\Helper\\Data', ['getWishlist', 'getCustomer', 'urlEncode'], [], '', false); $this->urlEncoder = $this->getMock('Magento\\Framework\\Url\\EncoderInterface', ['encode'], [], '', false); $this->wishlistHelper->expects($this->any())->method('getWishlist')->will($this->returnValue($wishlist)); $this->wishlistHelper->expects($this->any())->method('getCustomer')->will($this->returnValue($customer)); $this->urlEncoder->expects($this->any())->method('encode')->willReturnCallback(function ($url) { return strtr(base64_encode($url), '+/=', '-_,'); }); $this->urlBuilder = $this->getMock('Magento\\Framework\\App\\Rss\\UrlBuilderInterface'); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->link = $this->objectManagerHelper->getObject('Magento\\Wishlist\\Block\\Rss\\EmailLink', ['wishlistHelper' => $this->wishlistHelper, 'rssUrlBuilder' => $this->urlBuilder, 'urlEncoder' => $this->urlEncoder]); }
/** * Retrieve block cache tags * * @return array */ public function getIdentities() { /** @var $wishlist \Magento\Wishlist\Model\Wishlist */ $wishlist = $this->_wishlistHelper->getWishlist(); $identities = $wishlist->getIdentities(); foreach ($wishlist->getItemCollection() as $item) { /** @var $item \Magento\Wishlist\Model\Item */ $identities = array_merge($identities, $item->getProduct()->getIdentities()); } return $identities; }
/** * Test for method _toHtml for the case, when wishlist is absent */ public function testToHtmlWithoutWishlist() { $url = 'http://base.url/index'; $rssString = '<xml>Some empty xml</xml>'; $rssObjMock = $this->getMock('Magento\\Rss\\Model\\Rss', [], [], '', false); $customerServiceMock = $this->getMock('Magento\\Customer\\Service\\V1\\Data\\Customer', [], [], '', false); $wishlistModelMock = $this->getMock('Magento\\Wishlist\\Model\\Wishlist', ['getId', '__wakeup', 'getCustomerId'], [], '', false); $expectedHeaders = ['title' => __('We cannot retrieve the wish list.'), 'description' => __('We cannot retrieve the wish list.'), 'link' => $url, 'charset' => 'UTF-8']; $this->rssFactoryMock->expects($this->once())->method('create')->will($this->returnValue($rssObjMock)); $this->wishlistHelperMock->expects($this->once())->method('getWishlist')->will($this->returnValue($wishlistModelMock)); $wishlistModelMock->expects($this->once())->method('getId')->will($this->returnValue(false)); $this->urlBuilderMock->expects($this->once())->method('getUrl')->will($this->returnValue($url)); $this->wishlistHelperMock->expects($this->once())->method('getCustomer')->will($this->returnValue($customerServiceMock)); $rssObjMock->expects($this->once())->method('_addHeader')->with($expectedHeaders)->will($this->returnSelf()); $rssObjMock->expects($this->once())->method('createRssXml')->will($this->returnValue($rssString)); $this->assertEquals($rssString, $this->block->toHtml()); }
public function testGetSectionDataWithoutItems() { $items = []; $result = ['counter' => null, 'items' => []]; $this->wishlistHelperMock->expects($this->once())->method('getItemCount')->willReturn(count($items)); $this->viewMock->expects($this->never())->method('loadLayout'); $this->wishlistHelperMock->expects($this->never())->method('getWishlistItemCollection'); $this->catalogImageHelperMock->expects($this->never())->method('init'); $this->catalogImageHelperMock->expects($this->never())->method('getUrl'); $this->catalogImageHelperMock->expects($this->never())->method('getLabel'); $this->catalogImageHelperMock->expects($this->never())->method('getWidth'); $this->catalogImageHelperMock->expects($this->never())->method('getHeight'); $this->wishlistHelperMock->expects($this->never())->method('getProductUrl'); $this->sidebarMock->expects($this->never())->method('getProductPriceHtml'); $this->wishlistHelperMock->expects($this->never())->method('getAddToCartParams'); $this->wishlistHelperMock->expects($this->never())->method('getRemoveParams'); $this->assertEquals($result, $this->model->getSectionData()); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testExecuteWithoutQuantityArrayAndConfigurable() { $itemId = 2; $wishlistId = 1; $qty = []; $productId = 4; $indexUrl = 'index_url'; $configureUrl = 'configure_url'; $options = [5 => 'option']; $params = ['item' => $itemId, 'qty' => $qty]; $this->formKeyValidator->expects($this->once())->method('validate')->with($this->requestMock)->willReturn(true); $itemMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->disableOriginalConstructor()->setMethods(['load', 'getId', 'getWishlistId', 'setQty', 'setOptions', 'getBuyRequest', 'mergeBuyRequest', 'addToCart', 'getProduct', 'getProductId'])->getMock(); $this->requestMock->expects($this->at(0))->method('getParam')->with('item', null)->willReturn($itemId); $this->itemFactoryMock->expects($this->once())->method('create')->willReturn($itemMock); $itemMock->expects($this->once())->method('load')->with($itemId, null)->willReturnSelf(); $itemMock->expects($this->exactly(2))->method('getId')->willReturn($itemId); $itemMock->expects($this->once())->method('getWishlistId')->willReturn($wishlistId); $wishlistMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Wishlist')->disableOriginalConstructor()->getMock(); $this->wishlistProviderMock->expects($this->once())->method('getWishlist')->with($wishlistId)->willReturn($wishlistMock); $this->requestMock->expects($this->at(1))->method('getParam')->with('qty', null)->willReturn($qty); $this->quantityProcessorMock->expects($this->once())->method('process')->with(1)->willReturnArgument(0); $itemMock->expects($this->once())->method('setQty')->with(1)->willReturnSelf(); $this->urlMock->expects($this->at(0))->method('getUrl')->with('*/*', null)->willReturn($indexUrl); $itemMock->expects($this->once())->method('getProductId')->willReturn($productId); $this->urlMock->expects($this->at(1))->method('getUrl')->with('*/*/configure/', ['id' => $itemId, 'product_id' => $productId])->willReturn($configureUrl); $optionMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item\\Option')->disableOriginalConstructor()->getMock(); $this->optionFactoryMock->expects($this->once())->method('create')->willReturn($optionMock); $optionsMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\ResourceModel\\Item\\Option\\Collection')->disableOriginalConstructor()->getMock(); $optionMock->expects($this->once())->method('getCollection')->willReturn($optionsMock); $optionsMock->expects($this->once())->method('addItemFilter')->with([$itemId])->willReturnSelf(); $optionsMock->expects($this->once())->method('getOptionsByItem')->with($itemId)->willReturn($options); $itemMock->expects($this->once())->method('setOptions')->with($options)->willReturnSelf(); $this->requestMock->expects($this->once())->method('getParams')->willReturn($params); $buyRequestMock = $this->getMockBuilder('Magento\\Framework\\DataObject')->disableOriginalConstructor()->getMock(); $itemMock->expects($this->once())->method('getBuyRequest')->willReturn($buyRequestMock); $this->productHelperMock->expects($this->once())->method('addParamsToBuyRequest')->with($params, ['current_config' => $buyRequestMock])->willReturn($buyRequestMock); $itemMock->expects($this->once())->method('mergeBuyRequest')->with($buyRequestMock)->willReturnSelf(); $itemMock->expects($this->once())->method('addToCart')->with($this->checkoutCartMock, true)->willThrowException(new \Magento\Framework\Exception\LocalizedException(__('message'))); $this->messageManagerMock->expects($this->once())->method('addNotice')->with('message', null)->willReturnSelf(); $this->helperMock->expects($this->once())->method('calculate')->willReturnSelf(); $this->resultRedirectMock->expects($this->once())->method('setUrl')->with($configureUrl)->willReturnSelf(); $this->assertSame($this->resultRedirectMock, $this->model->execute()); }
/** * Get JSON POST params for moving from cart * * @return string */ public function getMoveFromCartParams() { return $this->wishlistHelper->getMoveFromCartParams($this->getItem()->getId()); }
/** * @param Observer $observer * @return void */ public function execute(Observer $observer) { $this->wishlistData->calculate(); }
/** * Move all wishlist item to cart * * @param Wishlist $wishlist * @param array $qtys * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function moveAllToCart(Wishlist $wishlist, $qtys) { $isOwner = $wishlist->isOwner($this->customerSession->getCustomerId()); $messages = []; $addedProducts = []; $notSalable = []; $cart = $this->cart; $collection = $wishlist->getItemCollection()->setVisibilityFilter(); foreach ($collection as $item) { /** @var $item \Magento\Wishlist\Model\Item */ try { $disableAddToCart = $item->getProduct()->getDisableAddToCart(); $item->unsProduct(); // Set qty if (isset($qtys[$item->getId()])) { $qty = $this->quantityProcessor->process($qtys[$item->getId()]); if ($qty) { $item->setQty($qty); } } $item->getProduct()->setDisableAddToCart($disableAddToCart); // Add to cart if ($item->addToCart($cart, $isOwner)) { $addedProducts[] = $item->getProduct(); } } catch (LocalizedException $e) { if ($e instanceof ProductException) { $notSalable[] = $item; } else { $messages[] = __('%1 for "%2".', trim($e->getMessage(), '.'), $item->getProduct()->getName()); } $cartItem = $cart->getQuote()->getItemByProduct($item->getProduct()); if ($cartItem) { $cart->getQuote()->deleteItem($cartItem); } } catch (\Exception $e) { $this->logger->critical($e); $messages[] = __('We can\'t add this item to your shopping cart right now.'); } } if ($isOwner) { $indexUrl = $this->helper->getListUrl($wishlist->getId()); } else { $indexUrl = $this->urlBuilder->getUrl('wishlist/shared', ['code' => $wishlist->getSharingCode()]); } if ($this->cartHelper->getShouldRedirectToCart()) { $redirectUrl = $this->cartHelper->getCartUrl(); } elseif ($this->redirector->getRefererUrl()) { $redirectUrl = $this->redirector->getRefererUrl(); } else { $redirectUrl = $indexUrl; } if ($notSalable) { $products = []; foreach ($notSalable as $item) { $products[] = '"' . $item->getProduct()->getName() . '"'; } $messages[] = __('We couldn\'t add the following product(s) to the shopping cart: %1.', join(', ', $products)); } if ($messages) { foreach ($messages as $message) { $this->messageManager->addError($message); } $redirectUrl = $indexUrl; } if ($addedProducts) { // save wishlist model for setting date of last update try { $wishlist->save(); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t update the Wish List right now.')); $redirectUrl = $indexUrl; } $products = []; foreach ($addedProducts as $product) { /** @var $product \Magento\Catalog\Model\Product */ $products[] = '"' . $product->getName() . '"'; } $this->messageManager->addSuccess(__('%1 product(s) have been added to shopping cart: %2.', count($addedProducts), join(', ', $products))); // save cart and collect totals $cart->save()->getQuote()->collectTotals(); } $this->helper->calculate(); return $redirectUrl; }
/** * Retrieve current wishlist * * @return \Magento\Wishlist\Model\Wishlist */ public function getWishlist() { return $this->_wishlistData->getWishlist(); }
/** * @param \Magento\Framework\App\Helper\Context $context * @param \Magento\Core\Helper\Data $coreData * @param \Magento\Framework\Registry $coreRegistry * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Wishlist\Model\WishlistFactory $wishlistFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Core\Helper\PostData $postDataHelper * @param \Magento\Customer\Helper\View $customerViewHelper * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider * @param \Magento\Customer\Service\V1\Data\CustomerBuilder $customerBuilder */ public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Core\Helper\Data $coreData, \Magento\Framework\Registry $coreRegistry, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Customer\Model\Session $customerSession, \Magento\Wishlist\Model\WishlistFactory $wishlistFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Core\Helper\PostData $postDataHelper, \Magento\Customer\Helper\View $customerViewHelper, \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider, \Magento\Customer\Service\V1\Data\CustomerBuilder $customerBuilder) { $this->_customerBuilder = $customerBuilder; parent::__construct($context, $coreData, $coreRegistry, $scopeConfig, $customerSession, $wishlistFactory, $storeManager, $postDataHelper, $customerViewHelper, $wishlistProvider); }
/** * @param \Magento\Framework\App\Helper\Context $context * @param \Magento\Framework\Registry $coreRegistry * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Wishlist\Model\WishlistFactory $wishlistFactory * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper * @param \Magento\Customer\Helper\View $customerViewHelper * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository * @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Customer\Model\Session $customerSession, \Magento\Wishlist\Model\WishlistFactory $wishlistFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Data\Helper\PostHelper $postDataHelper, \Magento\Customer\Helper\View $customerViewHelper, \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository) { $this->_customerFactory = $customerFactory; $this->_customerRepository = $customerRepository; parent::__construct($context, $coreRegistry, $customerSession, $wishlistFactory, $storeManager, $postDataHelper, $customerViewHelper, $wishlistProvider, $productRepository); }
/** * Retrieve add to wishlist params * * @param \Magento\Catalog\Model\Product $product * @return string */ public function getAddToWishlistParams($product) { return $this->_wishlistHelper->getAddParams($product); }
public function testGetWishlistWithCoreRegistry() { $wishlist = $this->getMock('\\Magento\\Wishlist\\Model\\Wishlist', [], [], '', false); $this->coreRegistry->expects($this->any())->method('registry')->will($this->returnValue($wishlist)); $this->assertEquals($wishlist, $this->wishlistHelper->getWishlist()); }
/** * Retrieve Wishlist model * * @return \Magento\Wishlist\Model\Wishlist */ protected function getWishlist() { $wishlist = $this->wishlistHelper->getWishlist(); return $wishlist; }
/** * Move all wishlist item to cart * * @param Wishlist $wishlist * @param array $qtys * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function moveAllToCart(Wishlist $wishlist, $qtys) { $isOwner = $wishlist->isOwner($this->customerSession->getCustomerId()); $messages = []; $addedItems = []; $notSalable = []; $hasOptions = []; $cart = $this->cart; $collection = $wishlist->getItemCollection()->setVisibilityFilter(); foreach ($collection as $item) { /** @var \Magento\Wishlist\Model\Item */ try { $disableAddToCart = $item->getProduct()->getDisableAddToCart(); $item->unsProduct(); // Set qty if (isset($qtys[$item->getId()])) { $qty = $this->quantityProcessor->process($qtys[$item->getId()]); if ($qty) { $item->setQty($qty); } } $item->getProduct()->setDisableAddToCart($disableAddToCart); // Add to cart if ($item->addToCart($cart, $isOwner)) { $addedItems[] = $item->getProduct(); } } catch (\Magento\Framework\Exception\LocalizedException $e) { if ($e->getCode() == \Magento\Wishlist\Model\Item::EXCEPTION_CODE_NOT_SALABLE) { $notSalable[] = $item; } elseif ($e->getCode() == \Magento\Wishlist\Model\Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS) { $hasOptions[] = $item; } else { $messages[] = __('%1 for "%2".', trim($e->getMessage(), '.'), $item->getProduct()->getName()); } $cartItem = $cart->getQuote()->getItemByProduct($item->getProduct()); if ($cartItem) { $cart->getQuote()->deleteItem($cartItem); } } catch (\Exception $e) { $this->logger->critical($e); $messages[] = __('We cannot add this item to your shopping cart.'); } } if ($isOwner) { $indexUrl = $this->helper->getListUrl($wishlist->getId()); } else { $indexUrl = $this->urlBuilder->getUrl('wishlist/shared', ['code' => $wishlist->getSharingCode()]); } if ($this->cartHelper->getShouldRedirectToCart()) { $redirectUrl = $this->cartHelper->getCartUrl(); } elseif ($this->redirector->getRefererUrl()) { $redirectUrl = $this->redirector->getRefererUrl(); } else { $redirectUrl = $indexUrl; } if ($notSalable) { $products = []; foreach ($notSalable as $item) { $products[] = '"' . $item->getProduct()->getName() . '"'; } $messages[] = __('We couldn\'t add the following product(s) to the shopping cart: %1.', join(', ', $products)); } if ($hasOptions) { $products = []; foreach ($hasOptions as $item) { $products[] = '"' . $item->getProduct()->getName() . '"'; } $messages[] = __('Product(s) %1 have required options. Each product can only be added individually.', join(', ', $products)); } if ($messages) { $isMessageSole = count($messages) == 1; if ($isMessageSole && count($hasOptions) == 1) { $item = $hasOptions[0]; if ($isOwner) { $item->delete(); } $redirectUrl = $item->getProductUrl(); } else { foreach ($messages as $message) { $this->messageManager->addError($message); } $redirectUrl = $indexUrl; } } if ($addedItems) { // save wishlist model for setting date of last update try { $wishlist->save(); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t update wish list.')); $redirectUrl = $indexUrl; } $products = []; foreach ($addedItems as $product) { $products[] = '"' . $product->getName() . '"'; } $this->messageManager->addSuccess(__('%1 product(s) have been added to shopping cart: %2.', count($addedItems), join(', ', $products))); // save cart and collect totals $cart->save()->getQuote()->collectTotals(); } $this->helper->calculate(); return $redirectUrl; }
/** * Retrieve wishlist item data * * @param \Magento\Wishlist\Model\Item $wishlistItem * @return array */ protected function getItemData(\Magento\Wishlist\Model\Item $wishlistItem) { $product = $wishlistItem->getProduct(); return ['image' => $this->getImageData($product), 'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem), 'product_name' => $product->getName(), 'product_price' => $this->block->getProductPriceHtml($product, \Magento\Catalog\Pricing\Price\ConfiguredPriceInterface::CONFIGURED_PRICE_CODE, \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST, ['item' => $wishlistItem]), 'product_is_saleable_and_visible' => $product->isSaleable() && $product->isVisibleInSiteVisibility(), 'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product), 'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem, true), 'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem, true)]; }
/** * Customer login processing * * @param \Magento\Framework\Event\Observer $observer * @return $this * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function customerLogin(\Magento\Framework\Event\Observer $observer) { $this->_wishlistData->calculate(); return $this; }
/** * Retrieve add to wishlist params * * @param Product $product * @return string */ public function getAddToWishlistParams($product) { $beforeCompareUrl = $this->_catalogSession->getBeforeCompareUrl(); $encodedUrl = array(\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl($beforeCompareUrl)); return $this->_wishlistHelper->getAddParams($product, $encodedUrl); }