Example #1
0
 /**
  * Add shared wishlist item to shopping cart
  *
  * If Product has required options - redirect
  * to product view page with message about needed defined required options
  *
  * @return \Magento\Framework\Controller\Result\Redirect
  */
 public function execute()
 {
     $itemId = (int) $this->getRequest()->getParam('item');
     /* @var $item Item */
     $item = $this->itemFactory->create()->load($itemId);
     $redirectUrl = $this->_redirect->getRefererUrl();
     try {
         /** @var OptionCollection $options */
         $options = $this->optionFactory->create()->getCollection()->addItemFilter([$itemId]);
         $item->setOptions($options->getOptionsByItem($itemId));
         $item->addToCart($this->cart);
         $this->cart->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->cartHelper->getShouldRedirectToCart()) {
             $redirectUrl = $this->cartHelper->getCartUrl();
         }
     } catch (ProductException $e) {
         $this->messageManager->addError(__('This product(s) is out of stock.'));
     } catch (LocalizedException $e) {
         $this->messageManager->addNotice($e->getMessage());
         $redirectUrl = $item->getProductUrl();
     } catch (\Exception $e) {
         $this->messageManager->addException($e, __('We can\'t add the item to the cart right now.'));
     }
     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     $resultRedirect->setUrl($redirectUrl);
     return $resultRedirect;
 }
 public function testGetConfigureUrl()
 {
     $json = '{json;}';
     /**
      * @var Item|\PHPUnit_Framework_MockObject_MockObject $itemMock
      */
     $itemMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item')->disableOriginalConstructor()->getMock();
     $this->cartHelperMock->expects($this->once())->method('getDeletePostJson')->with($itemMock)->willReturn($json);
     $this->model->setItem($itemMock);
     $this->assertEquals($json, $this->model->getDeletePostJson());
 }
 public function testGetAddToCartPostParams()
 {
     $url = 'http://localhost.com/dev/';
     $id = 1;
     $uenc = strtr(base64_encode($url), '+/=', '-_,');
     $expectedPostData = ['action' => $url, 'data' => ['product' => $id, 'uenc' => $uenc]];
     $this->typeInstanceMock->expects($this->once())->method('hasRequiredOptions')->with($this->equalTo($this->productMock))->will($this->returnValue(false));
     $this->cartHelperMock->expects($this->any())->method('getAddUrl')->with($this->equalTo($this->productMock), $this->equalTo([]))->will($this->returnValue($url));
     $this->productMock->expects($this->once())->method('getEntityId')->will($this->returnValue($id));
     $this->productMock->expects($this->once())->method('getTypeInstance')->will($this->returnValue($this->typeInstanceMock));
     $this->urlHelperMock->expects($this->once())->method('getEncodedUrl')->with($this->equalTo($url))->will($this->returnValue($uenc));
     $result = $this->block->getAddToCartPostParams($this->productMock);
     $this->assertEquals($expectedPostData, $result);
 }
 /**
  * Add quote messages
  *
  * @return void
  */
 public function addQuoteMessages()
 {
     // Compose array of messages to add
     $messages = [];
     /** @var \Magento\Framework\Message\MessageInterface $message */
     foreach ($this->cartHelper->getQuote()->getMessages() as $message) {
         if ($message) {
             // Escape HTML entities in quote message to prevent XSS
             $message->setText($this->escapeHtml($message->getText()));
             $messages[] = $message;
         }
     }
     $this->messageManager->addUniqueMessages($messages);
 }
 /**
  * Retrieve url for add product to cart
  * Will return product view page URL if product has required options
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param array $additional
  * @return string
  */
 public function getAddToCartUrl($product, $additional = [])
 {
     if ($product->getTypeInstance()->hasRequiredOptions($product)) {
         if (!isset($additional['_escape'])) {
             $additional['_escape'] = true;
         }
         if (!isset($additional['_query'])) {
             $additional['_query'] = [];
         }
         $additional['_query']['options'] = 'cart';
         return $this->getProductUrl($product, $additional);
     }
     return $this->_cartHelper->getAddUrl($product, $additional);
 }
Example #6
0
 /**
  * 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());
 }
 /**
  * Add quote messages
  *
  * @return void
  */
 protected function addQuoteMessages()
 {
     // Compose array of messages to add
     $messages = [];
     /** @var MessageInterface $message */
     foreach ($this->cartHelper->getQuote()->getMessages() as $message) {
         if (!$message->getIdentifier()) {
             try {
                 $messages[] = $this->messageManager->createMessage($message->getType())->setText($message->getText());
             } catch (\InvalidArgumentException $e) {
                 // pass
             }
         } else {
             $messages[] = $message;
         }
     }
     $this->messageManager->addUniqueMessages($messages);
 }
Example #8
0
 /**
  * @param bool $isAjax
  * @return array
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function prepareExecuteWithQuantityArray($isAjax = false)
 {
     $itemId = 2;
     $wishlistId = 1;
     $qty = [$itemId => 3];
     $productId = 4;
     $productName = 'product_name';
     $indexUrl = 'index_url';
     $configureUrl = 'configure_url';
     $options = [5 => 'option'];
     $params = ['item' => $itemId, 'qty' => $qty];
     $refererUrl = 'referer_url';
     $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($qty[$itemId])->willReturnArgument(0);
     $itemMock->expects($this->once())->method('setQty')->with($qty[$itemId])->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\\Resource\\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);
     $this->requestMock->expects($this->once())->method('isAjax')->willReturn($isAjax);
     $buyRequestMock = $this->getMockBuilder('Magento\\Framework\\Object')->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)->willReturn(true);
     $this->checkoutCartMock->expects($this->once())->method('save')->willReturnSelf();
     $quoteMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->setMethods(['getHasError', 'collectTotals'])->getMock();
     $this->checkoutCartMock->expects($this->exactly(2))->method('getQuote')->willReturn($quoteMock);
     $quoteMock->expects($this->once())->method('collectTotals')->willReturnSelf();
     $wishlistMock->expects($this->once())->method('save')->willReturnSelf();
     $quoteMock->expects($this->once())->method('getHasError')->willReturn(false);
     $productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock();
     $itemMock->expects($this->once())->method('getProduct')->willReturn($productMock);
     $productMock->expects($this->once())->method('getName')->willReturn($productName);
     $this->escaperMock->expects($this->once())->method('escapeHtml')->with($productName, null)->willReturn($productName);
     $this->messageManagerMock->expects($this->once())->method('addSuccess')->with('You added ' . $productName . ' to your shopping cart.', null)->willReturnSelf();
     $this->cartHelperMock->expects($this->once())->method('getShouldRedirectToCart')->willReturn(false);
     $this->redirectMock->expects($this->once())->method('getRefererUrl')->willReturn($refererUrl);
     $this->helperMock->expects($this->once())->method('calculate')->willReturnSelf();
     return $refererUrl;
 }
Example #9
0
 /**
  * @param integer $id
  * @param string $url
  * @param bool $isAjax
  * @param string $expectedPostData
  *
  * @dataProvider deletePostJsonDataProvider
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function testGetDeletePostJson($id, $url, $isAjax, $expectedPostData)
 {
     $item = $this->getMock('Magento\\Quote\\Model\\Quote\\Item', [], [], '', false);
     $item->expects($this->once())->method('getId')->will($this->returnValue($id));
     $this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue($isAjax));
     $this->urlBuilderMock->expects($this->any())->method('getCurrentUrl')->will($this->returnValue($url));
     $this->urlBuilderMock->expects($this->once())->method('getUrl')->will($this->returnValue($url));
     $result = $this->helper->getDeletePostJson($item);
     $this->assertEquals($expectedPostData, $result);
 }
Example #10
0
 public function testGetAddToCartPostParams()
 {
     $url = 'http://localhost.com/dev/';
     $id = 1;
     $uenc = strtr(base64_encode($url), '+/=', '-_,');
     $data = array('product' => $id, \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED => $uenc);
     $expectedPostData = json_encode(array('action' => $url, 'data' => array('product' => $id, 'uenc' => $uenc)));
     $this->typeInstanceMock->expects($this->once())->method('hasRequiredOptions')->with($this->equalTo($this->productMock))->will($this->returnValue(false));
     $this->cartHelperMock->expects($this->any())->method('getAddUrl')->with($this->equalTo($this->productMock), $this->equalTo(array()))->will($this->returnValue($url));
     $this->productMock->expects($this->once())->method('getEntityId')->will($this->returnValue($id));
     $this->productMock->expects($this->once())->method('getTypeInstance')->will($this->returnValue($this->typeInstanceMock));
     $this->postDataHelperMock->expects($this->once())->method('getEncodedUrl')->with($this->equalTo($url))->will($this->returnValue($uenc));
     $this->postDataHelperMock->expects($this->once())->method('getPostData')->with($this->equalTo($url), $this->equalTo($data))->will($this->returnValue($expectedPostData));
     $result = $this->block->getAddToCartPostParams($this->productMock);
     $this->assertEquals($expectedPostData, $result);
 }
Example #11
0
 public function testGetAddUrl()
 {
     $productEntityId = 1;
     $storeId = 1;
     $productMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getEntityId', 'hasUrlDataObject', 'getUrlDataObject', '__wakeup'], [], '', false);
     $productMock->expects($this->any())->method('getEntityId')->will($this->returnValue($productEntityId));
     $productMock->expects($this->any())->method('hasUrlDataObject')->will($this->returnValue(true));
     $productMock->expects($this->any())->method('getUrlDataObject')->will($this->returnValue(new Object(array('store_id' => $storeId))));
     $currentUrl = 'http://www.example.com/';
     $this->urlBuilderMock->expects($this->any())->method('getCurrentUrl')->will($this->returnValue($currentUrl));
     $this->coreHelperMock->expects($this->any())->method('urlEncode')->with($currentUrl)->will($this->returnValue($currentUrl));
     $this->requestMock->expects($this->any())->method('getRouteName')->will($this->returnValue('checkout'));
     $this->requestMock->expects($this->any())->method('getControllerName')->will($this->returnValue('cart'));
     $params = array(Action::PARAM_NAME_URL_ENCODED => $currentUrl, 'product' => $productEntityId, 'custom_param' => 'value', '_scope' => $storeId, '_scope_to_url' => true, 'in_cart' => 1);
     $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart/add', $params);
     $this->helper->getAddUrl($productMock, array('custom_param' => 'value'));
 }
Example #12
0
 /**
  * Retrieve url for direct adding product to cart
  *
  * @param \Magento\Catalog\Model\Product $product
  * @param array $additional
  * @return string
  */
 public function getAddToCartUrl($product, $additional = [])
 {
     return $this->_cartHelper->getAddUrl($product, $additional);
 }
Example #13
0
 /**
  * @param integer $id
  * @param string $url
  * @param bool $isAjax
  * @param string $expectedPostData
  *
  * @dataProvider deletePostJsonDataProvider
  */
 public function testGetDeletePostJson($id, $url, $isAjax, $expectedPostData)
 {
     $storeManager = $this->getMockForAbstractClass('\\Magento\\Store\\Model\\StoreManagerInterface');
     $coreData = $this->getMock('\\Magento\\Core\\Helper\\Data', [], [], '', false);
     $scopeConfig = $this->getMockForAbstractClass('\\Magento\\Framework\\App\\Config\\ScopeConfigInterface');
     $checkoutCart = $this->getMock('\\Magento\\Checkout\\Model\\Cart', [], [], '', false);
     $checkoutSession = $this->getMock('\\Magento\\Checkout\\Model\\Session', [], [], '', false);
     $context = $this->getMock('\\Magento\\Framework\\App\\Helper\\Context', [], [], '', false);
     $urlBuilder = $this->getMock('Magento\\Framework\\UrlInterface');
     $context->expects($this->once())->method('getUrlBuilder')->will($this->returnValue($urlBuilder));
     $item = $this->getMock('Magento\\Sales\\Model\\Quote\\Item', [], [], '', false);
     $request = $this->getMock('\\Magento\\Framework\\App\\Request\\Http', [], [], '', false);
     $context->expects($this->once())->method('getRequest')->will($this->returnValue($request));
     $helper = new Cart($context, $storeManager, $coreData, $scopeConfig, $checkoutCart, $checkoutSession);
     $item->expects($this->once())->method('getId')->will($this->returnValue($id));
     $request->expects($this->once())->method('isAjax')->will($this->returnValue($isAjax));
     $urlBuilder->expects($this->any())->method('getCurrentUrl')->will($this->returnValue($url));
     $urlBuilder->expects($this->once())->method('getUrl')->will($this->returnValue($url));
     $result = $helper->getDeletePostJson($item);
     $this->assertEquals($expectedPostData, $result);
 }
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testMoveAllToCartWithException()
 {
     $wishlistId = 7;
     $sessionCustomerId = 23;
     $itemOneId = 14;
     $itemTwoId = 17;
     $productOneName = 'product one';
     $productTwoName = 'product two';
     $qtys = [14 => 21];
     $isOwner = true;
     $indexUrl = 'index_url';
     /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemOneMock */
     $itemOneMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])->disableOriginalConstructor()->getMock();
     /** @var \Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemTwoMock */
     $itemTwoMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->setMethods(['getProduct', 'unsProduct', 'getId', 'setQty', 'addToCart', 'delete', 'getProductUrl'])->disableOriginalConstructor()->getMock();
     /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productOneMock */
     $productOneMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])->disableOriginalConstructor()->getMock();
     /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $productTwoMock */
     $productTwoMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->setMethods(['getDisableAddToCart', 'setDisableAddToCart', 'getName'])->disableOriginalConstructor()->getMock();
     $itemOneMock->expects($this->any())->method('getProduct')->willReturn($productOneMock);
     $itemTwoMock->expects($this->any())->method('getProduct')->willReturn($productTwoMock);
     $collection = [$itemOneMock, $itemTwoMock];
     /** @var \Magento\Wishlist\Model\Wishlist|\PHPUnit_Framework_MockObject_MockObject $wishlistMock */
     $wishlistMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\Wishlist')->disableOriginalConstructor()->getMock();
     $this->sessionMock->expects($this->once())->method('getCustomerId')->willReturn($sessionCustomerId);
     $wishlistMock->expects($this->once())->method('isOwner')->with($sessionCustomerId)->willReturn($isOwner);
     $wishlistMock->expects($this->once())->method('getId')->willReturn($wishlistId);
     /** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
     $collectionMock = $this->getMockBuilder('Magento\\Wishlist\\Model\\ResourceModel\\Item\\Collection')->disableOriginalConstructor()->getMock();
     $wishlistMock->expects($this->once())->method('getItemCollection')->willReturn($collectionMock);
     $collectionMock->expects($this->once())->method('setVisibilityFilter')->with(true)->willReturn($collection);
     $productOneMock->expects($this->once())->method('getDisableAddToCart')->willReturn(true);
     $productOneMock->expects($this->once())->method('setDisableAddToCart')->with(true);
     $productTwoMock->expects($this->once())->method('getDisableAddToCart')->willReturn(false);
     $productTwoMock->expects($this->once())->method('setDisableAddToCart')->with(false);
     $itemOneMock->expects($this->once())->method('unsProduct');
     $itemTwoMock->expects($this->once())->method('unsProduct');
     $itemOneMock->expects($this->exactly(2))->method('getId')->willReturn($itemOneId);
     $itemTwoMock->expects($this->once())->method('getId')->willReturn($itemTwoId);
     $this->quantityProcessorMock->expects($this->once())->method('process')->with($qtys[$itemOneId])->willReturnArgument(0);
     $itemOneMock->expects($this->once())->method('setQty')->with($qtys[$itemOneId])->willReturnSelf();
     $itemTwoMock->expects($this->never())->method('setQty');
     $itemOneMock->expects($this->once())->method('addToCart')->with($this->cartMock, $isOwner)->willReturn(true);
     $exception = new \Exception('Exception.');
     $itemTwoMock->expects($this->once())->method('addToCart')->with($this->cartMock, $isOwner)->willThrowException($exception);
     $this->loggerMock->expects($this->once())->method('critical')->with($exception, []);
     $this->managerMock->expects($this->at(0))->method('addError')->with(__('We can\'t add this item to your shopping cart right now.'), null)->willReturnSelf();
     $this->wishlistHelperMock->expects($this->once())->method('getListUrl')->with($wishlistId)->willReturn($indexUrl);
     $this->cartHelperMock->expects($this->once())->method('getShouldRedirectToCart')->with(null)->willReturn(false);
     $this->redirectMock->expects($this->once())->method('getRefererUrl')->willReturn('');
     $wishlistMock->expects($this->once())->method('save')->willThrowException(new \Exception());
     $this->managerMock->expects($this->at(1))->method('addError')->with(__('We can\'t update the Wish List right now.'), null)->willReturnSelf();
     $productOneMock->expects($this->any())->method('getName')->willReturn($productOneName);
     $productTwoMock->expects($this->any())->method('getName')->willReturn($productTwoName);
     $this->managerMock->expects($this->once())->method('addSuccess')->with(__('%1 product(s) have been added to shopping cart: %2.', 1, '"' . $productOneName . '"'), null)->willReturnSelf();
     $this->cartMock->expects($this->once())->method('save')->willReturnSelf();
     /** @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
     $quoteMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->getMock();
     $this->cartMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
     $quoteMock->expects($this->once())->method('collectTotals')->willReturnSelf();
     $this->wishlistHelperMock->expects($this->once())->method('calculate')->willReturnSelf();
     $this->assertEquals($indexUrl, $this->model->moveAllToCart($wishlistMock, $qtys));
 }
Example #15
0
 /**
  * Count items in cart
  *
  * @return int
  */
 protected function _getItemCount()
 {
     $count = $this->getSummaryQty();
     return $count ? $count : $this->_cartHelper->getSummaryCount();
 }
Example #16
0
 /**
  * @return bool
  */
 public function getIsVirtual()
 {
     return $this->_cartHelper->getIsVirtualQuote();
 }
Example #17
0
 /**
  * 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 \Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     $itemId = (int) $this->getRequest()->getParam('item');
     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     /* @var $item \Magento\Wishlist\Model\Item */
     $item = $this->itemFactory->create()->load($itemId);
     if (!$item->getId()) {
         $resultRedirect->setPath('*/*');
         return $resultRedirect;
     }
     $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
     if (!$wishlist) {
         $resultRedirect->setPath('*/*');
         return $resultRedirect;
     }
     // 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->cartHelper->getShouldRedirectToCart()) {
             $redirectUrl = $this->cartHelper->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, __('We can\'t add the item to the cart right now.'));
     }
     $this->helper->calculate();
     if ($this->getRequest()->isAjax()) {
         /** @var \Magento\Framework\Controller\Result\Json $resultJson */
         $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
         $resultJson->setData(['backUrl' => $redirectUrl]);
         return $resultJson;
     }
     $resultRedirect->setUrl($redirectUrl);
     return $resultRedirect;
 }
Example #18
0
 /**
  * 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;
 }
Example #19
0
 /**
  * Get delete item POST JSON
  *
  * @return string
  * @codeCoverageIgnore
  */
 public function getDeletePostJson()
 {
     return $this->cartHelper->getDeletePostJson($this->getItem());
 }
 /**
  * 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;
 }