/** * Creates a sample order * * @throws \Magento\Framework\Exception\LocalizedException */ public function createOrder() { $store = $this->storeManager->getStore(); $websiteId = $this->storeManager->getStore()->getWebsiteId(); $customer = $this->getCustomer(); $repositoryCustomer = $this->customerRepository->getById($customer->getId()); $quote = $this->quoteFactory->create(); $quote->setStore($store); $quote->assignCustomer($repositoryCustomer); $this->addItemsToQuote($quote); $quote->getBillingAddress()->addData($this->orderSampleData['address']); $quote->getShippingAddress()->addData($this->orderSampleData['address']); $this->addressRate->setCode(self::ORDER_SHIPPING_METHOD_CODE); $this->addressRate->getPrice(1); $shippingAddress = $quote->getShippingAddress(); $shippingAddress->addShippingRate($this->addressRate); $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod(self::ORDER_SHIPPING_METHOD_CODE); $quote->setPaymentMethod(self::ORDER_PAYMENT_METHOD_CODE); $quote->setInventoryProcessed(false); $quote->save(); $quote->getPayment()->importData(['method' => self::ORDER_PAYMENT_METHOD_CODE]); $quote->collectTotals()->save(); $order = $this->quoteManagement->submit($quote); $order->setEmailSent(0); }
/** * Load data for customer quote and merge with current quote * * @return $this */ public function loadCustomerQuote() { if (!$this->_customerSession->getCustomerId()) { return $this; } $this->_eventManager->dispatch('load_customer_quote_before', ['checkout_session' => $this]); try { $customerQuote = $this->quoteRepository->getForCustomer($this->_customerSession->getCustomerId()); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $customerQuote = $this->quoteFactory->create(); } $customerQuote->setStoreId($this->_storeManager->getStore()->getId()); if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) { if ($this->getQuoteId()) { $this->quoteRepository->save($customerQuote->merge($this->getQuote())->collectTotals()); } $this->setQuoteId($customerQuote->getId()); if ($this->_quote) { $this->quoteRepository->delete($this->_quote); } $this->_quote = $customerQuote; } else { $this->getQuote()->getBillingAddress(); $this->getQuote()->getShippingAddress(); $this->getQuote()->setCustomer($this->_customerSession->getCustomerDataObject())->setTotalsCollectedFlag(false)->collectTotals(); $this->quoteRepository->save($this->getQuote()); } return $this; }
/** * Load quote and replace in session. */ public function _loadAndReplace() { $checkoutSession = $this->checkoutSession->create(); $quote = $this->quoteFactory->create()->load($this->quote->getId()); $quote->setIsActive(true)->save(); $checkoutSession->replaceQuote($quote); }
/** * Retrieve quote model object * * @return \Magento\Quote\Model\Quote */ public function getQuote() { if ($this->_quote === null) { $this->_quote = $this->quoteFactory->create(); if ($this->getStoreId()) { if (!$this->getQuoteId()) { $this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId()); $this->_quote->setIsActive(false); $this->_quote->setStoreId($this->getStoreId()); $this->quoteRepository->save($this->_quote); $this->setQuoteId($this->_quote->getId()); $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]); } else { $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]); $this->_quote->setStoreId($this->getStoreId()); } if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) { $customer = $this->customerRepository->getById($this->getCustomerId()); $this->_quote->assignCustomer($customer); $this->quoteRepository->save($this->_quote); } } $this->_quote->setIgnoreOldQty(true); $this->_quote->setIsSuperMode(true); } return $this->_quote; }
public function execute() { $params = $this->getRequest()->getParams(); $session = $this->_getCheckout(); $session->setQuoteId($session->getAssistQuoteId(true)); $orderId = $params['ordernumber']; $order = $this->_getOrder($orderId); $quote = $this->_quoteFactory->create()->load($order->getQuoteId()); $txnId = $params['billnumber']; if ($order->getBaseTotalDue() && $quote->getId()) { $order->registerCancellation(__('Customer payment on ASSIST failed.'))->save(); $this->messageManager->addSuccessMessage(__('Sorry, your transaction is failed and cannot be' . ' processed, please choose another payment method' . ' or contact Customer Care to complete' . ' your order.')); if ($this->_getCheckout()->restoreQuote()) { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); return $resultRedirect->setPath('checkout/cart'); } } $this->_logger->debug('cancel'); }
/** * Loads customer, quote and quote item by request params * * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ protected function _initData() { $this->_customerId = (int) $this->getRequest()->getParam('customer_id'); if (!$this->_customerId) { throw new \Magento\Framework\Exception\LocalizedException(__('No customer ID defined.')); } $quoteItemId = (int) $this->getRequest()->getParam('id'); $websiteId = (int) $this->getRequest()->getParam('website_id'); try { $this->_quote = $this->quoteRepository->getForCustomer($this->_customerId); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $this->_quote = $this->quoteFactory->create(); } $this->_quote->setWebsite($this->_objectManager->get('Magento\\Store\\Model\\StoreManagerInterface')->getWebsite($websiteId)); $this->_quoteItem = $this->_quote->getItemById($quoteItemId); if (!$this->_quoteItem) { throw new LocalizedException(__('Please correct the quote items and try again.')); } return $this; }
/** * @expectedExcpetion \Magento\Framework\Exception\NoSuchEntityException * @expectedExceptionMessage No such entity with cartId = 15 */ public function testGetWithExceptionByIsActive() { $cartId = 15; $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock); $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock); $this->storeMock->expects($this->once())->method('getId')->willReturn($this->storeMock); $this->quoteMock->expects($this->never())->method('setSharedStoreIds'); $this->quoteMock->expects($this->once())->method('load')->with($cartId)->willReturn($this->storeMock); $this->quoteMock->expects($this->once())->method('getId')->willReturn($cartId); $this->quoteMock->expects($this->once())->method('getIsActive')->willReturn(0); $this->model->get($cartId); }
/** * Get the quote of the cart * * @return \Magento\Quote\Model\Quote */ protected function getQuote() { if (null === $this->quote) { $customerId = $this->getCustomerId(); $storeIds = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds(); try { $this->quote = $this->quoteRepository->getForCustomer($customerId, $storeIds); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds); } } return $this->quote; }
/** * Basket items. * * @return mixed */ public function getBasketItems() { $params = $this->getRequest()->getParams(); if (!isset($params['quote_id']) || !isset($params['code'])) { $this->helper->log('Basket no quote id or code is set'); return false; } $quoteId = $params['quote_id']; $quoteModel = $this->quoteFactory->create()->loadByIdWithoutStore($quoteId); //check for any quote for this email, don't want to render further if (!$quoteModel->getId()) { $this->helper->log('no quote found for ' . $quoteId); return false; } if (!$quoteModel->getIsActive()) { $this->helper->log('Cart is not active : ' . $quoteId); return false; } $this->quote = $quoteModel; //Start environment emulation of the specified store $storeId = $quoteModel->getStoreId(); $appEmulation = $this->emulationFactory->create(); $appEmulation->startEnvironmentEmulation($storeId); $quoteItems = $quoteModel->getAllItems(); $itemsData = []; foreach ($quoteItems as $quoteItem) { //skip configurable products if ($quoteItem->getParentItemId() != null) { continue; } $_product = $quoteItem->getProduct(); $inStock = $_product->isInStock() ? 'In Stock' : 'Out of stock'; $total = $this->priceHelper->currency($quoteItem->getPrice()); $productUrl = $_product->getProductUrl(); $grandTotal = $this->priceHelper->currency($this->getGrandTotal()); $itemsData[] = ['grandTotal' => $grandTotal, 'total' => $total, 'inStock' => $inStock, 'productUrl' => $productUrl, 'product' => $_product, 'qty' => $quoteItem->getQty()]; } return $itemsData; }
/** * Load quote with different methods * * @param string $loadMethod * @param string $loadField * @param int $identifier * @param int[] $sharedStoreIds * @throws NoSuchEntityException * @return Quote */ protected function loadQuote($loadMethod, $loadField, $identifier, array $sharedStoreIds = []) { /** @var Quote $quote */ $quote = $this->quoteFactory->create(); if ($sharedStoreIds) { $quote->setSharedStoreIds($sharedStoreIds); } $quote->setStoreId($this->storeManager->getStore()->getId())->{$loadMethod}($identifier); if (!$quote->getId()) { throw NoSuchEntityException::singleField($loadField, $identifier); } return $quote; }
public function testGetActiveForCustomer() { $cartId = 17; $customerId = 23; $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock); $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock); $this->storeMock->expects($this->once())->method('getId')->willReturn(1); $this->quoteMock->expects($this->never())->method('setSharedStoreIds'); $this->quoteMock->expects($this->once())->method('loadByCustomer')->with($customerId)->willReturn($this->storeMock); $this->quoteMock->expects($this->exactly(2))->method('getId')->willReturn($cartId); $this->quoteMock->expects($this->exactly(2))->method('getIsActive')->willReturn(1); $this->assertEquals($this->quoteMock, $this->model->getActiveForCustomer($customerId)); $this->assertEquals($this->quoteMock, $this->model->getActiveForCustomer($customerId)); }
/** * Get quote * * @return \Magento\Quote\Model\Quote */ protected function getQuote() { if (null == $this->quote) { $storeIds = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds(); $this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds); $currentCustomerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID); if (!empty($currentCustomerId)) { try { $this->quote = $this->quoteRepository->getForCustomer($currentCustomerId, $storeIds); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { } } } return $this->quote; }
/** * Run test getQuote method * * @return void * @dataProvider getQuoteDataProvider */ public function testGetQuoteWithQuoteId($customerId, $quoteCustomerId, $expectedNumberOfInvokes) { $quoteId = 22; $storeId = 10; $this->quote->expects($this->any())->method('getQuoteId')->will($this->returnValue($quoteId)); $this->quote->expects($this->any())->method('setQuoteId')->with($quoteId); $this->quote->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId)); $this->quote->expects($this->any())->method('getCustomerId')->will($this->returnValue($customerId)); $dataCustomerMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock(); $this->customerRepositoryMock->expects($this->{$expectedNumberOfInvokes}())->method('getById')->with($customerId)->willReturn($dataCustomerMock); $quoteMock = $this->getMock('Magento\\Quote\\Model\\Quote', ['setStoreId', 'setCustomerGroupId', 'setIsActive', 'getId', 'assignCustomer', 'setIgnoreOldQty', 'setIsSuperMode', 'getCustomerId', '__wakeup'], [], '', false); $quoteMock->expects($this->once())->method('setStoreId')->with($storeId); $quoteMock->expects($this->{$expectedNumberOfInvokes}())->method('assignCustomer')->with($dataCustomerMock); $quoteMock->expects($this->once())->method('setIgnoreOldQty')->with(true); $quoteMock->expects($this->once())->method('setIsSuperMode')->with(true); $quoteMock->expects($this->once())->method('getCustomerId')->will($this->returnValue($quoteCustomerId)); $this->quoteRepositoryMock->expects($this->once())->method('get')->with($quoteId)->willReturn($quoteMock); $this->assertEquals($quoteMock, $this->quote->getQuote()); }
/** * Get quote products to show feefo reviews. * * @return array */ public function getQuoteProducts() { $products = []; $quoteId = $this->_request->getParam('quote_id'); /** @var \Magento\Quote\Model\Quote $quoteModel */ $quoteModel = $this->quoteFactory->create()->load($quoteId); if (!$quoteModel->getId()) { return []; } $quoteItems = $quoteModel->getAllItems(); if (count($quoteItems) == 0) { return []; } foreach ($quoteItems as $item) { $productModel = $item->getProduct(); if ($productModel->getId()) { $products[$productModel->getSku()] = $productModel->getName(); } } return $products; }
/** * @return \Magento\Quote\Model\Quote */ protected function _createQuote() { return $this->_quoteFactory->create(); }
/** * Get the products to display for table. * * @return array */ public function getLoadedProductCollection() { //products to be diplayd for recommended pages $productsToDisplay = []; $productsToDisplayCounter = 0; $quoteId = $this->getRequest()->getParam('quote_id'); //display mode based on the action name $mode = $this->getRequest()->getActionName(); $quoteModel = $this->quoteFactory->create()->load($quoteId); //number of product items to be displayed $limit = $this->recommendedHelper->getDisplayLimitByMode($mode); $quoteItems = $quoteModel->getAllItems(); $numItems = count($quoteItems); //no product found to display if ($numItems == 0 || !$limit) { return []; } elseif ($numItems > $limit) { $maxPerChild = 1; } else { $maxPerChild = number_format($limit / $numItems); } $this->helper->log('DYNAMIC QUOTE PRODUCTS : limit ' . $limit . ' products : ' . $numItems . ', max per child : ' . $maxPerChild); foreach ($quoteItems as $item) { $i = 0; //parent product $productModel = $item->getProduct(); //check for product exists if ($productModel->getId()) { //get single product for current mode $recommendedProducts = $this->_getRecommendedProduct($productModel, $mode); foreach ($recommendedProducts as $product) { //check if still exists if ($product->getId() && $productsToDisplayCounter < $limit && $i <= $maxPerChild && $product->isSaleable() && !$product->getParentId()) { //we have a product to display $productsToDisplay[$product->getId()] = $product; $i++; $productsToDisplayCounter++; } } } //have reached the limit don't loop for more if ($productsToDisplayCounter == $limit) { break; } } //check for more space to fill up the table with fallback products if ($productsToDisplayCounter < $limit) { $fallbackIds = $this->recommendedHelper->getFallbackIds(); $productCollection = $this->productFactory->create()->getCollection()->addIdFilter($fallbackIds)->addAttributeToSelect(['product_url', 'name', 'store_id', 'small_image', 'price']); foreach ($productCollection as $product) { if ($product->isSaleable()) { $productsToDisplay[$product->getId()] = $product; } //stop the limit was reached //@codingStandardsIgnoreStart if (count($productsToDisplay) == $limit) { break; } //@codingStandardsIgnoreEnd } } $this->helper->log('quote - loaded product to display ' . count($productsToDisplay)); return $productsToDisplay; }
/** * Creates a cart for the currently logged-in customer. * * @param int $customerId * @param int $storeId * @return \Magento\Quote\Model\Quote Cart object. * @throws CouldNotSaveException The cart could not be created. */ protected function createCustomerCart($customerId, $storeId) { $customer = $this->customerRepository->getById($customerId); try { $quote = $this->quoteRepository->getActiveForCustomer($customerId); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteFactory->create(); $quote->setStoreId($storeId); $quote->setCustomer($customer); $quote->setCustomerIsGuest(0); } return $quote; }
/** * Retrieve customer cart quote object model * * @return \Magento\Quote\Model\Quote */ public function getCustomerCart() { if (!is_null($this->_cart)) { return $this->_cart; } $this->_cart = $this->quoteFactory->create(); $customerId = (int) $this->getSession()->getCustomerId(); if ($customerId) { try { $this->_cart = $this->quoteRepository->getForCustomer($customerId); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $this->_cart->setStore($this->getSession()->getStore()); $customerData = $this->customerRepository->getById($customerId); $this->_cart->assignCustomer($customerData); $this->quoteRepository->save($this->_cart); } } return $this->_cart; }