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