/** * {@inheritDoc} * * @param int $cartId The cart ID. * @return Totals Quote totals data. */ public function get($cartId) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); $shippingAddress = $quote->getShippingAddress(); if ($quote->isVirtual()) { $totalsData = array_merge($quote->getBillingAddress()->getData(), $quote->getData()); } else { $totalsData = array_merge($shippingAddress->getData(), $quote->getData()); } $totals = $this->totalsFactory->create(); $this->dataObjectHelper->populateWithArray($totals, $totalsData, '\\Magento\\Quote\\Api\\Data\\TotalsInterface'); $items = []; $weeeTaxAppliedAmount = 0; foreach ($quote->getAllVisibleItems() as $index => $item) { $items[$index] = $this->itemConverter->modelToDataObject($item); $weeeTaxAppliedAmount += $item->getWeeeTaxAppliedAmount(); } $totals->setCouponCode($this->couponService->get($cartId)); $calculatedTotals = $this->totalsConverter->process($quote->getTotals()); $amount = $totals->getGrandTotal() - $totals->getTaxAmount(); $amount = $amount > 0 ? $amount : 0; $totals->setGrandTotal($amount); $totals->setTotalSegments($calculatedTotals); $totals->setItems($items); $totals->setWeeeTaxAppliedAmount($weeeTaxAppliedAmount); return $totals; }
/** * @param CartTotalRepository $subject * @param \Closure $proceed * @param int $cartId * @return \Magento\Quote\Model\Cart\Totals * @throws \Magento\Framework\Exception\NoSuchEntityException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundGet(CartTotalRepository $subject, \Closure $proceed, $cartId) { $result = $proceed($cartId); $quote = $this->quoteRepository->getActive($cartId); $totals = $quote->getTotals(); if (!array_key_exists('tax', $totals)) { return $result; } $taxes = $totals['tax']->getData(); if (!array_key_exists('full_info', $taxes)) { return $result; } $detailsId = 1; $finalData = []; foreach ($taxes['full_info'] as $info) { if (array_key_exists('hidden', $info) && $info['hidden'] || $info['amount'] == 0 && $this->taxConfig->displayCartZeroTax()) { continue; } $taxDetails = $this->detailsFactory->create([]); $taxDetails->setAmount($info['amount']); $taxRates = $this->getRatesData($info['rates']); $taxDetails->setRates($taxRates); $taxDetails->setGroupId($detailsId); $finalData[] = $taxDetails; $detailsId++; } $attributes = $result->getExtensionAttributes(); if ($attributes === null) { $attributes = $this->extensionFactory->create(); } $attributes->setTaxGrandtotalDetails($finalData); /** @var $result \Magento\Quote\Model\Cart\Totals */ $result->setExtensionAttributes($attributes); return $result; }
/** * @param \Magento\Checkout\Model\ShippingInformationManagement $subject * @param $cartId * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation */ public function beforeSaveAddressInformation(\Magento\Checkout\Model\ShippingInformationManagement $subject, $cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation) { $customFee = $addressInformation->getExtensionAttributes()->getFee(); $quote = $this->quoteRepository->getActive($cartId); if ($customFee) { $fee = $this->dataHelper->getCustomFee(); $quote->setFee($fee); } else { $quote->setFee(NULL); } }
/** * {@inheritDoc} * * @param int $cartId The cart ID. * @return Totals Quote totals data. */ public function get($cartId) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); $shippingAddress = $quote->getShippingAddress(); $totalsData = array_merge($shippingAddress->getData(), $quote->getData()); $totals = $this->totalsFactory->create(); $this->dataObjectHelper->populateWithArray($totals, $totalsData, '\\Magento\\Quote\\Api\\Data\\TotalsInterface'); $totals->setItems($quote->getAllItems()); return $totals; }
/** * {@inheritdoc} */ public function placeOrder($cartId, $agreements = null, PaymentInterface $paymentMethod = null) { $quote = $this->quoteRepository->getActive($cartId); if ($paymentMethod) { $paymentMethod->setChecks([\Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_CHECKOUT, \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_COUNTRY, \Magento\Payment\Model\Method\AbstractMethod::CHECK_USE_FOR_CURRENCY, \Magento\Payment\Model\Method\AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX, \Magento\Payment\Model\Method\AbstractMethod::CHECK_ZERO_TOTAL]); $quote->getPayment()->setQuote($quote); $data = $paymentMethod->getData(); if (isset($data['additional_data'])) { $data = array_merge($data, (array) $data['additional_data']); unset($data['additional_data']); } $quote->getPayment()->importData($data); } if ($quote->getCheckoutMethod() === self::METHOD_GUEST) { $quote->setCustomerId(null); $quote->setCustomerEmail($quote->getBillingAddress()->getEmail()); $quote->setCustomerIsGuest(true); $quote->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID); } $order = $this->submit($quote); $this->checkoutSession->setLastQuoteId($quote->getId()); $this->checkoutSession->setLastSuccessQuoteId($quote->getId()); $this->checkoutSession->setLastOrderId($order->getId()); $this->checkoutSession->setLastRealOrderId($order->getIncrementId()); $this->checkoutSession->setLastOrderStatus($order->getStatus()); $this->eventManager->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $quote]); return $order->getId(); }
/** * {@inheritDoc} * * @param int $cartId The shopping cart ID. * @param string $carrierCode The carrier code. * @param string $methodCode The shipping method code. * @return bool * @throws \Magento\Framework\Exception\InputException The shipping method is not valid for an empty cart. * @throws \Magento\Framework\Exception\CouldNotSaveException The shipping method could not be saved. * @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart contains only virtual products and the shipping method is not applicable. * @throws \Magento\Framework\Exception\StateException The billing or shipping address is not set. */ public function set($cartId, $carrierCode, $methodCode) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (0 == $quote->getItemsCount()) { throw new InputException(__('Shipping method is not applicable for empty cart')); } if ($quote->isVirtual()) { throw new NoSuchEntityException(__('Cart contains virtual product(s) only. Shipping method is not applicable.')); } $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress->getCountryId()) { throw new StateException(__('Shipping address is not set')); } $billingAddress = $quote->getBillingAddress(); if (!$billingAddress->getCountryId()) { throw new StateException(__('Billing address is not set')); } $shippingAddress->setShippingMethod($carrierCode . '_' . $methodCode); if (!$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())) { throw new NoSuchEntityException(__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode)); } try { $this->quoteRepository->save($quote->collectTotals()); } catch (\Exception $e) { throw new CouldNotSaveException(__('Cannot set shipping method. %1', $e->getMessage())); } return true; }
/** * {@inheritdoc} */ public function getActive($cartId, array $sharedStoreIds = array()) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getActive'); if (!$pluginInfo) { return parent::getActive($cartId, $sharedStoreIds); } else { return $this->___callPlugins('getActive', func_get_args(), $pluginInfo); } }
/** * {@inheritDoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function saveAddressInformation($cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation) { $address = $addressInformation->getShippingAddress(); $carrierCode = $addressInformation->getShippingCarrierCode(); $methodCode = $addressInformation->getShippingMethodCode(); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if ($quote->isVirtual()) { throw new NoSuchEntityException(__('Cart contains virtual product(s) only. Shipping address is not applicable.')); } if (0 == $quote->getItemsCount()) { throw new InputException(__('Shipping method is not applicable for empty cart')); } $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0; $sameAsBilling = $address->getSameAsBilling() ? 1 : 0; $customerAddressId = $address->getCustomerAddressId(); $this->addressValidator->validate($address); $quote->setShippingAddress($address); $address = $quote->getShippingAddress(); if ($customerAddressId) { $addressData = $this->addressRepository->getById($customerAddressId); $address = $quote->getShippingAddress()->importCustomerAddressData($addressData); } $address->setSameAsBilling($sameAsBilling); $address->setSaveInAddressBook($saveInAddressBook); $address->setCollectShippingRates(true); if (!$address->getCountryId()) { throw new StateException(__('Shipping address is not set')); } $address->setShippingMethod($carrierCode . '_' . $methodCode); try { $address->save(); $address->collectTotals(); } catch (\Exception $e) { $this->logger->critical($e); throw new InputException(__('Unable to save address. Please, check input data.')); } if (!$address->getShippingRateByCode($address->getShippingMethod())) { throw new NoSuchEntityException(__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode)); } if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) { throw new InputException($this->scopeConfig->getValue('sales/minimum_order/error_message', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $quote->getStoreId())); } try { $address->save(); $quote->collectTotals(); $this->quoteRepository->save($quote); } catch (\Exception $e) { $this->logger->critical($e); throw new InputException(__('Unable to save shipping information. Please, check input data.')); } /** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */ $paymentDetails = $this->paymentDetailsFactory->create(); $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId)); $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId)); return $paymentDetails; }
/** * {@inheritDoc} */ public function get($cartId) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if ($quote->isVirtual()) { throw new NoSuchEntityException(__('Cart contains virtual product(s) only. Shipping address is not applicable.')); } /** * Address. * * @var \Magento\Quote\Model\Quote\Address $address */ return $quote->getShippingAddress(); }
/** * {@inheritdoc} */ public function remove($cartId) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (!$quote->getItemsCount()) { throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId)); } $quote->getShippingAddress()->setCollectShippingRates(true); try { $quote->setCouponCode(''); $this->quoteRepository->save($quote->collectTotals()); } catch (\Exception $e) { throw new CouldNotDeleteException(__('Could not delete coupon code')); } if ($quote->getCouponCode() != '') { throw new CouldNotDeleteException(__('Could not delete coupon code')); } return true; }
/** * {@inheritdoc} */ public function placeOrder($cartId) { $quote = $this->quoteRepository->getActive($cartId); if ($quote->getCheckoutMethod() === 'guest') { $quote->setCustomerId(null); $quote->setCustomerEmail($quote->getBillingAddress()->getEmail()); $quote->setCustomerIsGuest(true); $quote->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID); } return $this->submit($quote)->getId(); }
/** * {@inheritDoc} */ public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (0 == $quote->getItemsCount()) { throw new InputException(__('Gift Messages is not applicable for empty cart')); } if ($quote->isVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('quote', $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $this->giftMessageManager->setMessage($quote, 'quote', $giftMessage); return true; }
/** * {@inheritDoc} */ public function estimateByAddressId($cartId, $addressId) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); // no methods applicable for empty carts or carts with virtual products if ($quote->isVirtual() || 0 == $quote->getItemsCount()) { return []; } $address = $this->addressRepository->getById($addressId); return $this->getEstimatedRates($quote, $address->getCountryId(), $address->getPostcode(), $address->getRegionId(), $address->getRegion()); }
/** * @{inheritdoc} */ public function saveAddresses($cartId, \Magento\Quote\Api\Data\AddressInterface $billingAddress, \Magento\Quote\Api\Data\AddressInterface $shippingAddress = null, \Magento\Quote\Api\Data\AddressAdditionalDataInterface $additionalData = null, $checkoutMethod = null) { $this->billingAddressManagement->assign($cartId, $billingAddress); /** @var \Magento\Quote\Api\Data\AddressDetailsInterface $addressDetails */ $addressDetails = $this->addressDetailsFactory->create(); if ($shippingAddress) { $this->shippingAddressManagement->assign($cartId, $shippingAddress); $addressDetails->setFormattedShippingAddress($this->shippingAddressManagement->get($cartId)->format('html')); $addressDetails->setShippingMethods($this->shippingMethodManagement->getList($cartId)); } $addressDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId)); if ($additionalData !== null) { $this->dataProcessor->process($additionalData); } if ($checkoutMethod != null) { $this->quoteRepository->save($this->quoteRepository->getActive($cartId)->setCheckoutMethod($checkoutMethod)); } $addressDetails->setFormattedBillingAddress($this->billingAddressManagement->get($cartId)->format('html')); $addressDetails->setTotals($this->cartTotalsRepository->get($cartId)); return $addressDetails; }
public function testGetActiveWithSharedStoreIds() { $cartId = 16; $sharedStoreIds = [1, 2]; $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->once())->method('setSharedStoreIds')->with($sharedStoreIds)->willReturnSelf(); $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->exactly(2))->method('getIsActive')->willReturn(1); $this->assertEquals($this->quoteMock, $this->model->getActive($cartId, $sharedStoreIds)); $this->assertEquals($this->quoteMock, $this->model->getActive($cartId, $sharedStoreIds)); }
/** * {@inheritDoc} * * @param int $cartId The cart ID. * @return Totals Quote totals data. */ public function get($cartId) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); $shippingAddress = $quote->getShippingAddress(); if ($quote->isVirtual()) { $totalsData = array_merge($quote->getBillingAddress()->getData(), $quote->getData()); } else { $totalsData = array_merge($shippingAddress->getData(), $quote->getData()); } $totals = $this->totalsFactory->create(); $this->dataObjectHelper->populateWithArray($totals, $totalsData, '\\Magento\\Quote\\Api\\Data\\TotalsInterface'); $items = []; foreach ($quote->getAllVisibleItems() as $index => $item) { $items[$index] = $this->converter->modelToDataObject($item); } $totals->setItems($items); return $totals; }
/** * {@inheritdoc} */ public function deleteById($cartId, $itemId) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); $quoteItem = $quote->getItemById($itemId); if (!$quoteItem) { throw new NoSuchEntityException(__('Cart %1 doesn\'t contain item %2', $cartId, $itemId)); } try { $quote->removeItem($itemId); $this->quoteRepository->save($quote->collectTotals()); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not remove item from quote')); } return true; }
/** * {@inheritDoc} */ public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage, $itemId) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (!($item = $quote->getItemById($itemId))) { throw new NoSuchEntityException(__('There is no product with provided itemId: %1 in the cart', $itemId)); } if ($item->getIsVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('items', $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $this->giftMessageManager->setMessage($quote, 'quote_item', $giftMessage, $itemId); return true; }
/** * {@inheritDoc} */ public function get($cartId) { $cart = $this->quoteRepository->getActive($cartId); return $cart->getBillingAddress(); }
/** * {@inheritdoc} */ public function getList($cartId) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); return $this->methodList->getAvailableMethods($quote); }
/** * Get checkout quote instance by current session * * @return Quote * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getQuote() { $this->_eventManager->dispatch('custom_quote_process', ['checkout_session' => $this]); if ($this->_quote === null) { $quote = $this->quoteRepository->create(); if ($this->getQuoteId()) { try { if ($this->_loadInactive) { $quote = $this->quoteRepository->get($this->getQuoteId()); } else { $quote = $this->quoteRepository->getActive($this->getQuoteId()); } /** * If current currency code of quote is not equal current currency code of store, * need recalculate totals of quote. It is possible if customer use currency switcher or * store switcher. */ if ($quote->getQuoteCurrencyCode() != $this->_storeManager->getStore()->getCurrentCurrencyCode()) { $quote->setStore($this->_storeManager->getStore()); $this->quoteRepository->save($quote->collectTotals()); /* * We mast to create new quote object, because collectTotals() * can to create links with other objects. */ $quote = $this->quoteRepository->get($this->getQuoteId()); } } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $this->setQuoteId(null); } } if (!$this->getQuoteId()) { if ($this->_customerSession->isLoggedIn() || $this->_customer) { $customerId = $this->_customer ? $this->_customer->getId() : $this->_customerSession->getCustomerId(); try { $quote = $this->quoteRepository->getActiveForCustomer($customerId); $this->setQuoteId($quote->getId()); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { } } else { $quote->setIsCheckoutCart(true); $this->_eventManager->dispatch('checkout_quote_init', ['quote' => $quote]); } } if ($this->getQuoteId()) { if ($this->_customer) { $quote->setCustomer($this->_customer); } elseif ($this->_customerSession->isLoggedIn()) { $quote->setCustomer($this->customerRepository->getById($this->_customerSession->getCustomerId())); } } $quote->setStore($this->_storeManager->getStore()); $this->_quote = $quote; } $remoteAddress = $this->_remoteAddress->getRemoteAddress(); if ($remoteAddress) { $this->_quote->setRemoteIp($remoteAddress); $xForwardIp = $this->request->getServer('HTTP_X_FORWARDED_FOR'); $this->_quote->setXForwardedFor($xForwardIp); } return $this->_quote; }