/** * @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(), []); }
/** * 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; }
/** * @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.')); } }