Exemplo n.º 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;
 }
Exemplo n.º 2
0
 /**
  * Add catalog product object data to wishlist
  *
  * @param   \Magento\Catalog\Model\Product $product
  * @param   int $qty
  * @param   bool $forciblySetQty
  *
  * @return  Item
  */
 protected function _addCatalogProduct(\Magento\Catalog\Model\Product $product, $qty = 1, $forciblySetQty = false)
 {
     $item = null;
     foreach ($this->getItemCollection() as $_item) {
         if ($_item->representProduct($product)) {
             $item = $_item;
             break;
         }
     }
     if ($item === null) {
         $storeId = $product->hasWishlistStoreId() ? $product->getWishlistStoreId() : $this->getStore()->getId();
         $item = $this->_wishlistItemFactory->create();
         $item->setProductId($product->getId());
         $item->setWishlistId($this->getId());
         $item->setAddedAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT));
         $item->setStoreId($storeId);
         $item->setOptions($product->getCustomOptions());
         $item->setProduct($product);
         $item->setQty($qty);
         $item->save();
         if ($item->getId()) {
             $this->getItemCollection()->addItem($item);
         }
     } else {
         $qty = $forciblySetQty ? $qty : $item->getQty() + $qty;
         $item->setQty($qty)->save();
     }
     $this->addItem($item);
     return $item;
 }
Exemplo n.º 3
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 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);
 }
Exemplo n.º 4
0
 /**
  * @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());
 }
Exemplo n.º 5
0
 /**
  * @param int|\Magento\Wishlist\Model\Item|\PHPUnit_Framework_MockObject_MockObject $itemId
  * @param \Magento\Framework\Object $buyRequest
  * @param null|array|\Magento\Framework\Object $param
  * @throws \Magento\Framework\Exception\LocalizedException
  *
  * @dataProvider updateItemDataProvider
  */
 public function testUpdateItem($itemId, $buyRequest, $param)
 {
     $storeId = 1;
     $productId = 1;
     $stores = [(new \Magento\Framework\Object())->setId($storeId)];
     $newItem = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->disableOriginalConstructor()->getMock();
     $newItem->expects($this->any())->method('setProductId')->will($this->returnSelf());
     $newItem->expects($this->any())->method('setWishlistId')->will($this->returnSelf());
     $newItem->expects($this->any())->method('setStoreId')->will($this->returnSelf());
     $newItem->expects($this->any())->method('setOptions')->will($this->returnSelf());
     $newItem->expects($this->any())->method('setProduct')->will($this->returnSelf());
     $newItem->expects($this->any())->method('setQty')->will($this->returnSelf());
     $newItem->expects($this->any())->method('getItem')->will($this->returnValue(2));
     $this->itemFactory->expects($this->once())->method('create')->will($this->returnValue($newItem));
     $this->storeManager->expects($this->any())->method('getStores')->will($this->returnValue($stores));
     $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($stores[0]));
     $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock();
     $product->expects($this->any())->method('getId')->will($this->returnValue($productId));
     $product->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId));
     $instanceType = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type\\AbstractType')->disableOriginalConstructor()->getMock();
     $instanceType->expects($this->once())->method('processConfiguration')->will($this->returnValue($this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock()));
     $newProduct = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->getMock();
     $newProduct->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf());
     $newProduct->expects($this->once())->method('getTypeInstance')->will($this->returnValue($instanceType));
     $item = $this->getMockBuilder('Magento\\Wishlist\\Model\\Item')->disableOriginalConstructor()->getMock();
     $item->expects($this->once())->method('getProduct')->will($this->returnValue($product));
     $items = $this->getMockBuilder('Magento\\Wishlist\\Model\\Resource\\Item\\Collection')->disableOriginalConstructor()->getMock();
     $items->expects($this->once())->method('addWishlistFilter')->will($this->returnSelf());
     $items->expects($this->once())->method('addStoreFilter')->will($this->returnSelf());
     $items->expects($this->once())->method('setVisibilityFilter')->will($this->returnSelf());
     $items->expects($this->once())->method('getItemById')->will($this->returnValue($item));
     $items->expects($this->any())->method('getIterator')->will($this->returnValue(new \ArrayIterator([$item])));
     $this->itemsFactory->expects($this->any())->method('create')->will($this->returnValue($items));
     $this->productRepository->expects($this->once())->method('getById')->with($productId, false, $storeId)->will($this->returnValue($newProduct));
     $this->assertInstanceOf('Magento\\Wishlist\\Model\\Wishlist', $this->wishlist->updateItem($itemId, $buyRequest, $param));
 }