123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Quote\Model\Cart;
- use Magento\Quote\Api;
- use Magento\Quote\Api\CartRepositoryInterface;
- use Magento\Quote\Api\CartTotalRepositoryInterface;
- use Magento\Catalog\Helper\Product\ConfigurationPool;
- use Magento\Framework\Api\DataObjectHelper;
- use Magento\Framework\Api\ExtensibleDataInterface;
- use Magento\Quote\Model\Cart\Totals\ItemConverter;
- use Magento\Quote\Api\CouponManagementInterface;
- /**
- * Cart totals data object.
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class CartTotalRepository implements CartTotalRepositoryInterface
- {
- /**
- * Cart totals factory.
- *
- * @var Api\Data\TotalsInterfaceFactory
- */
- private $totalsFactory;
- /**
- * Quote repository.
- *
- * @var \Magento\Quote\Api\CartRepositoryInterface
- */
- private $quoteRepository;
- /**
- * @var \Magento\Framework\Api\DataObjectHelper
- */
- private $dataObjectHelper;
- /**
- * @var ConfigurationPool
- */
- private $itemConverter;
- /**
- * @var CouponManagementInterface
- */
- protected $couponService;
- /**
- * @var TotalsConverter
- */
- protected $totalsConverter;
- /**
- * @param Api\Data\TotalsInterfaceFactory $totalsFactory
- * @param CartRepositoryInterface $quoteRepository
- * @param DataObjectHelper $dataObjectHelper
- * @param CouponManagementInterface $couponService
- * @param TotalsConverter $totalsConverter
- * @param ItemConverter $converter
- */
- public function __construct(
- Api\Data\TotalsInterfaceFactory $totalsFactory,
- CartRepositoryInterface $quoteRepository,
- DataObjectHelper $dataObjectHelper,
- CouponManagementInterface $couponService,
- TotalsConverter $totalsConverter,
- ItemConverter $converter
- ) {
- $this->totalsFactory = $totalsFactory;
- $this->quoteRepository = $quoteRepository;
- $this->dataObjectHelper = $dataObjectHelper;
- $this->couponService = $couponService;
- $this->totalsConverter = $totalsConverter;
- $this->itemConverter = $converter;
- }
- /**
- * @inheritdoc
- *
- * @param int $cartId The cart ID.
- * @return Totals Quote totals data.
- */
- public function get($cartId)
- {
- /** @var \Magento\Quote\Model\Quote $quote */
- $quote = $this->quoteRepository->getActive($cartId);
- if ($quote->isVirtual()) {
- $addressTotalsData = $quote->getBillingAddress()->getData();
- $addressTotals = $quote->getBillingAddress()->getTotals();
- } else {
- $addressTotalsData = $quote->getShippingAddress()->getData();
- $addressTotals = $quote->getShippingAddress()->getTotals();
- }
- unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
- /** @var \Magento\Quote\Api\Data\TotalsInterface $quoteTotals */
- $quoteTotals = $this->totalsFactory->create();
- $this->dataObjectHelper->populateWithArray(
- $quoteTotals,
- $addressTotalsData,
- \Magento\Quote\Api\Data\TotalsInterface::class
- );
- $items = [];
- foreach ($quote->getAllVisibleItems() as $index => $item) {
- $items[$index] = $this->itemConverter->modelToDataObject($item);
- }
- $calculatedTotals = $this->totalsConverter->process($addressTotals);
- $quoteTotals->setTotalSegments($calculatedTotals);
- $amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
- $amount = $amount > 0 ? $amount : 0;
- $quoteTotals->setCouponCode($this->couponService->get($cartId));
- $quoteTotals->setGrandTotal($amount);
- $quoteTotals->setItems($items);
- $quoteTotals->setItemsQty($quote->getItemsQty());
- $quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
- $quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
- return $quoteTotals;
- }
- }
|