Files
2026-09-15 10:26:34 +08:00

113 lines
3.9 KiB
PHP

<?php
namespace Stemcell\Checkout\Model\Cart;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\ExtensibleDataInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Quote\Api;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\CouponManagementInterface;
use Magento\Quote\Model\Cart\Totals\ItemConverter;
use Magento\Quote\Model\Cart\TotalsConverter;
use Psr\Log\LoggerInterface;
use Stemcell\Checkout\Api\Data\Quote\TotalsInterface;
use Stemcell\Checkout\Api\Data\Quote\TotalsInterfaceFactory;
use Stemcell\Checkout\Model\Cart\Totals\Item;
class CartTotalRepository extends \Magento\Quote\Model\Cart\CartTotalRepository
{
private TotalsInterfaceFactory $totalsInterfaceFactory;
/**
* Cart totals factory.
*
* @var Api\Data\TotalsInterfaceFactory
*/
private $totalsFactory;
/**
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
private $quoteRepository;
/**
* @var \Magento\Framework\Api\DataObjectHelper
*/
private $dataObjectHelper;
/**
* @var ItemConverter
*/
private $itemConverter;
/**
* @var CouponManagementInterface
*/
protected $couponService;
/**
* @var TotalsConverter
*/
protected $totalsConverter;
public function __construct(
Api\Data\TotalsInterfaceFactory $totalsFactory,
TotalsInterfaceFactory $totalsInterfaceFactory,
CartRepositoryInterface $quoteRepository,
DataObjectHelper $dataObjectHelper,
CouponManagementInterface $couponService,
TotalsConverter $totalsConverter,
ItemConverter $converter
) {
parent::__construct($totalsFactory, $quoteRepository, $dataObjectHelper, $couponService, $totalsConverter, $converter);
$this->totalsInterfaceFactory = $totalsInterfaceFactory;
$this->totalsFactory = $totalsFactory;
$this->quoteRepository = $quoteRepository;
$this->dataObjectHelper = $dataObjectHelper;
$this->couponService = $couponService;
$this->totalsConverter = $totalsConverter;
$this->itemConverter = $converter;
}
/**
* @param $cartId
* @return TotalsInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function get($cartId): TotalsInterface
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if ($quote->isVirtual()) {
$quote->collectTotals();
$addressTotalsData = $quote->getBillingAddress()->getData();
$addressTotals = $quote->getBillingAddress()->getTotals();
} else {
$addressTotalsData = $quote->getShippingAddress()->getData();
$addressTotals = $quote->getShippingAddress()->getTotals();
}
unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
$quoteTotals = $this->totalsInterfaceFactory->create();
$this->dataObjectHelper->populateWithArray(
$quoteTotals,
$addressTotalsData,
TotalsInterface::class
);
$items = array_map([$this->itemConverter, 'modelToDataObject'], $quote->getAllVisibleItems());
// $logger = ObjectManager::getInstance()->get(LoggerInterface::class);
$calculatedTotals = $this->totalsConverter->process($addressTotals);
$quoteTotals->setTotalSegments($calculatedTotals);
$quoteTotals->setCouponCode($this->couponService->get($cartId));
$quoteTotals->setItems($items);
$quoteTotals->setDiscountAmount($quote->getData('customer_discount'));
$quoteTotals->setItemsQty($quote->getItemsQty());
$quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
$quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
return $quoteTotals;// TODO: Change the autogenerated stub
}
}