Example #1
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());
 }
Example #2
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;
 }
Example #3
0
 public function testUpdateQuoteItemWithZeroQty()
 {
     $itemId = 1;
     $itemQty = 0;
     $this->resolverMock->expects($this->never())->method('getLocale');
     $this->cartMock->expects($this->once())->method('updateItems')->with([$itemId => ['qty' => $itemQty]])->willReturnSelf();
     $this->cartMock->expects($this->once())->method('save')->willReturnSelf();
     $this->assertEquals($this->sidebar, $this->sidebar->updateQuoteItem($itemId, $itemQty));
 }
Example #4
0
 /**
  * Get shopping cart items qty based on configuration (summary qty or items qty)
  *
  * @return int|float
  */
 protected function getSummaryCount()
 {
     if (!$this->summeryCount) {
         $this->summeryCount = $this->checkoutCart->getSummaryQty() ?: 0;
     }
     return $this->summeryCount;
 }
Example #5
0
 /**
  * Get shopping cart items qty based on configuration (summary qty or items qty)
  *
  * @return int|float
  */
 public function getSummaryCount()
 {
     if ($this->getData('summary_qty')) {
         return $this->getData('summary_qty');
     }
     return $this->_checkoutCart->getSummaryQty();
 }
Example #6
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);
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function save()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'save');
     if (!$pluginInfo) {
         return parent::save();
     } else {
         return $this->___callPlugins('save', func_get_args(), $pluginInfo);
     }
 }
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 boolean $useQty
  * @dataProvider useQtyDataProvider
  */
 public function testGetSummaryQty($useQty)
 {
     $quoteId = 1;
     $itemsCount = 1;
     $quoteMock = $this->getMock('Magento\\Quote\\Model\\Quote', ['getItemsCount', 'getItemsQty', '__wakeup'], [], '', false);
     $this->checkoutSessionMock->expects($this->any())->method('getQuote')->will($this->returnValue($quoteMock));
     $this->checkoutSessionMock->expects($this->at(2))->method('getQuoteId')->will($this->returnValue($quoteId));
     $this->customerSessionMock->expects($this->any())->method('isLoggedIn')->will($this->returnValue(true));
     $this->scopeConfigMock->expects($this->once())->method('getValue')->with('checkout/cart_link/use_qty', \Magento\Framework\Store\ScopeInterface::SCOPE_STORE)->will($this->returnValue($useQty));
     $qtyMethodName = $useQty ? 'getItemsQty' : 'getItemsCount';
     $quoteMock->expects($this->once())->method($qtyMethodName)->will($this->returnValue($itemsCount));
     $this->assertEquals($itemsCount, $this->cart->getSummaryQty());
 }
Example #10
0
 /**
  * Get checkout method
  *
  * @return string
  */
 public function getCheckoutMethod()
 {
     if ($this->_cart->getCustomerSession()->isLoggedIn()) {
         return \Magento\Checkout\Model\Type\Onepage::METHOD_CUSTOMER;
     }
     if (!$this->_cart->getQuote()->getCheckoutMethod()) {
         if ($this->_checkoutData->isAllowedGuestCheckout($this->_cart->getQuote())) {
             $this->_cart->getQuote()->setCheckoutMethod(\Magento\Checkout\Model\Type\Onepage::METHOD_GUEST);
         } else {
             $this->_cart->getQuote()->setCheckoutMethod(\Magento\Checkout\Model\Type\Onepage::METHOD_REGISTER);
         }
     }
     return $this->_cart->getQuote()->getCheckoutMethod();
 }
Example #11
0
 /**
  * Returns uniqueId - required for duplicate request check, if the transaction was canceled
  *
  * @param CheckoutCart $cart
  *
  * @return string
  */
 protected function _getUniqueId($cart)
 {
     $uniqueId = $cart->getCustomerSession()->getUniqueId();
     if (!strlen($uniqueId)) {
         $uniqueId = $this->_generateUniqString();
         $cart->getCustomerSession()->setUniqueId($uniqueId);
     }
     return $uniqueId;
 }
Example #12
0
 public function execute()
 {
     $redirectTo = 'checkout/cart';
     $defaultErrorMessage = $this->_dataHelper->__('An error occurred during the payment process.');
     try {
         $this->_logger->debug(__METHOD__ . ':' . print_r($this->_request->getPost()->toArray(), true));
         $this->_cart->getCustomerSession()->unsUniqueId();
         if (!$this->_request->isPost()) {
             throw new \Exception('Not a post request');
         }
         $return = \WirecardCEE_QPay_ReturnFactory::getInstance($this->_request->getPost()->toArray(), $this->_dataHelper->getConfigData('basicdata/secret'));
         if (!$return->validate()) {
             throw new \Exception('Validation error: invalid response');
         }
         if (!strlen($return->mage_orderId)) {
             throw new \Exception('Magento OrderId is missing');
         }
         if (!strlen($return->mage_quoteId)) {
             throw new \Exception('Magento QuoteId is missing');
         }
         $orderId = $this->_request->getPost('mage_orderId');
         /** @var \Magento\Sales\Model\Order $order */
         $order = $this->_objectManager->create('\\Magento\\Sales\\Model\\Order');
         $order->loadByIncrementId($orderId);
         $orderExists = (bool) $order->getId();
         if ($return->mage_orderCreation == 'before') {
             if (!$orderExists) {
                 throw new \Exception('Order not found');
             }
             $payment = $order->getPayment();
             if (!strlen($payment->getAdditionalInformation('paymentState'))) {
                 $this->_logger->debug(__METHOD__ . ':order not processed via confirm server2server request, check your packetfilter!');
                 $order = $this->_orderManagement->processOrder($return);
             }
         }
         if ($return->mage_orderCreation == 'after') {
             if (!$orderExists && ($return->getPaymentState() == \WirecardCEE_QPay_ReturnFactory::STATE_SUCCESS || $return->getPaymentState() == \WirecardCEE_QPay_ReturnFactory::STATE_PENDING)) {
                 $this->_logger->debug(__METHOD__ . ':order not processed via confirm server2server request, check your packetfilter!');
                 $order = $this->_orderManagement->processOrder($return);
             }
         }
         switch ($return->getPaymentState()) {
             case \WirecardCEE_QPay_ReturnFactory::STATE_SUCCESS:
             case \WirecardCEE_QPay_ReturnFactory::STATE_PENDING:
                 if ($return->getPaymentState() == \WirecardCEE_QPay_ReturnFactory::STATE_PENDING) {
                     $this->messageManager->addNoticeMessage($this->_dataHelper->__('Your order will be processed as soon as we receive the payment confirmation from your bank.'));
                 }
                 /* needed for success page otherwise magento redirects to cart */
                 $this->_checkoutSession->setLastQuoteId($order->getQuoteId());
                 $this->_checkoutSession->setLastSuccessQuoteId($order->getQuoteId());
                 $this->_checkoutSession->setLastOrderId($order->getId());
                 $this->_checkoutSession->setLastRealOrderId($order->getIncrementId());
                 $this->_checkoutSession->setLastOrderStatus($order->getStatus());
                 $redirectTo = 'checkout/onepage/success';
                 break;
             case \WirecardCEE_QPay_ReturnFactory::STATE_CANCEL:
                 /** @var \WirecardCEE_QPay_Return_Cancel $return */
                 $this->messageManager->addNoticeMessage($this->_dataHelper->__('You have canceled the payment process!'));
                 if ($return->mage_orderCreation == 'before') {
                     $quote = $this->_orderManagement->reOrder($return->mage_quoteId);
                     $this->_checkoutSession->replaceQuote($quote)->unsLastRealOrderId();
                 }
                 break;
             case \WirecardCEE_QPay_ReturnFactory::STATE_FAILURE:
                 /** @var \WirecardCEE_QPay_Return_Failure $return */
                 $msg = $return->getErrors()->getConsumerMessage();
                 if (!strlen($msg)) {
                     $msg = $defaultErrorMessage;
                 }
                 $this->messageManager->addErrorMessage($msg);
                 if ($return->mage_orderCreation == 'before') {
                     $quote = $this->_orderManagement->reOrder($return->mage_quoteId);
                     $this->_checkoutSession->replaceQuote($quote)->unsLastRealOrderId();
                 }
                 break;
             default:
                 throw new \Exception('Unhandled Wirecard Checkout Page payment state:' . $return->getPaymentState());
         }
         if ($this->_request->getPost('iframeUsed')) {
             $redirectUrl = $this->_url->getUrl($redirectTo);
             $page = $this->_resultPageFactory->create();
             $page->getLayout()->getBlock('checkout.back')->addData(['redirectUrl' => $redirectUrl]);
             return $page;
         } else {
             $this->_redirect($redirectTo);
         }
     } catch (\Exception $e) {
         if (!$this->messageManager->getMessages()->getCount()) {
             $this->messageManager->addErrorMessage($defaultErrorMessage);
         }
         $this->_logger->debug(__METHOD__ . ':' . $e->getMessage());
         $this->_redirect($redirectTo);
     }
 }
Example #13
0
 /**
  * Disable multishipping
  *
  * @param \Magento\Framework\App\Action\Action $subject
  * @return void
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function beforeExecuteInternal(\Magento\Framework\App\Action\Action $subject)
 {
     $this->cart->getQuote()->setIsMultiShipping(0);
 }
 /**
  * @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
 /**
  * @magentoDataFixture Magento/Checkout/_files/simple_product.php
  * @magentoDataFixture Magento/Checkout/_files/set_product_min_in_cart.php
  * @magentoDbIsolation enabled
  */
 public function testAddProductWithNoQty()
 {
     $product = $this->productRepository->get('simple');
     $this->cart->addProduct($product->getId(), []);
 }
Example #16
0
 /**
  * Retrieve subtotal block html
  *
  * @return string
  */
 protected function getSubtotalHtml()
 {
     $totals = $this->cart->getQuote()->getTotals();
     $subtotal = isset($totals['subtotal']) && $totals['subtotal'] instanceof Total ? $totals['subtotal']->getValue() : 0;
     return $this->helperData->formatPrice($subtotal);
 }
Example #17
0
 /**
  * Add or Move item product to shopping cart
  *
  * Return true if product was successful added or exception with code
  * Return false for disabled or unvisible products
  *
  * @param \Magento\Checkout\Model\Cart $cart
  * @param bool $delete  delete the item after successful add to cart
  * @return bool
  * @throws \Magento\Catalog\Model\Product\Exception
  */
 public function addToCart(\Magento\Checkout\Model\Cart $cart, $delete = false)
 {
     $product = $this->getProduct();
     $storeId = $this->getStoreId();
     if ($product->getStatus() != \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) {
         return false;
     }
     if (!$product->isVisibleInSiteVisibility()) {
         if ($product->getStoreId() == $storeId) {
             return false;
         }
         $urlData = $this->_catalogUrl->getRewriteByProductStore([$product->getId() => $storeId]);
         if (!isset($urlData[$product->getId()])) {
             return false;
         }
         $product->setUrlDataObject(new \Magento\Framework\Object($urlData));
         $visibility = $product->getUrlDataObject()->getVisibility();
         if (!in_array($visibility, $product->getVisibleInSiteVisibilities())) {
             return false;
         }
     }
     if (!$product->isSalable()) {
         throw new ProductException(__('Product is not salable.'));
     }
     $buyRequest = $this->getBuyRequest();
     $cart->addProduct($product, $buyRequest);
     if (!$product->isVisibleInSiteVisibility()) {
         $cart->getQuote()->getItemByProduct($product)->setStoreId($storeId);
     }
     if ($delete) {
         $this->delete();
     }
     return true;
 }
Example #18
0
 /**
  * @return \Magento\Framework\View\Result\PageFactory
  */
 public function execute()
 {
     $pId = $this->getRequest()->getParam('pid');
     //productId
     $sdcnt = $this->getRequest()->getParam('sdcnt');
     //shoppree discount
     /* 
      $resultPage = $this->resultPageFactory->create();
      $resultPage->getConfig()->getTitle()->prepend(__('Shoppree Offers'));
      return $resultPage; 
     
      $layout = $this->_view->getLayout();
      $block = $layout->createBlock('Shoppree\Offers\Block\OfferBlock');
      echo $block->getCurrentStoreName();
      echo '<br>';
      echo $block->getProductName('1');
     */
     $layout = $this->_view->getLayout();
     $block = $layout->createBlock('Shoppree\\Offers\\Block\\OfferBlock');
     try {
         if (!empty($pId)) {
             $params = array();
             $params['qty'] = '1';
             //product quantity
             /*get product id*/
             //$pId = '1';//productId
             $_product = $this->product->load($pId);
             if ($_product) {
                 $quote = $this->cart->getQuote();
                 //Create object of quote
                 $_product->setPrice('0.00');
                 //$_product->setFinalPrice('0.00');
                 $this->cart->addProduct($_product, $params);
                 $this->cart->setItemsQty();
                 //$quote->setGrandTotal($this->cart->getQuote()->getGrandTotal() - 50.00);
                 //$this->cart->setQuote($quote);
                 $this->cart->saveQuote();
                 $this->_checkoutSession->setCartWasUpdated(false);
                 $this->cart->save();
                 $this->_checkoutSession->unsShoppreeDiscount();
                 $this->messageManager->addSuccess(__('Add to cart successfully.'));
             }
         } else {
             if (!empty($sdcnt)) {
                 /*sets shoppree discount*/
                 $this->_checkoutSession->setShoppreeDiscount($sdcnt);
                 /*$this->getResponse()->setRedirect('/magento-prototype/checkout/cart');*/
             }
         }
         //$this->messageManager->addSuccess(__('Add to cart successfully.').'--'.$this->_checkoutSession->getQuote()->getSubtotal().'--'.$this->_checkoutSession->getQuote()->getGrandTotal().'--'.$this->cart->getQuote()->getGrandTotal().'--'.$this->cart->getQuote()->getSubtotal());
         //$this->messageManager->addSuccess(__('Add to cart successfully.').$block->getGrandTotal().'--'.$this->cart->getQuote()->getGrandTotal());
         /*
          $this->messageManager->addSuccess(__('Add to cart successfully.').$this->cart->getItemsCount().'--'.$this->cart->getItemsQty()); 
             		 $this->getResponse()->setRedirect('/magento-prototype/checkout/cart');
         */
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $this->messageManager->addException($e, __('%1', $e->getMessage()));
     } catch (\Exception $e) {
         $this->messageManager->addException($e, __('error.'));
     }
 }