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

130 lines
6.2 KiB
PHP

<?php
namespace Stemcell\Checkout\Model;
use Magento\Captcha\Api\CaptchaConfigPostProcessorInterface;
use Magento\Catalog\Helper\Product\ConfigurationPool;
use Magento\Checkout\Helper\Data as CheckoutHelper;
use Magento\Checkout\Model\Cart;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
use Magento\Customer\Model\Address\CustomerAddressDataProvider;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Customer\Model\Url as CustomerUrlManager;
use Magento\Eav\Api\AttributeOptionManagementInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Escaper;
use Magento\Framework\Locale\FormatInterface as LocaleFormat;
use Magento\Framework\UrlInterface;
use Magento\Quote\Api\CartItemRepositoryInterface as QuoteItemRepository;
use Magento\Quote\Api\CartTotalRepositoryInterface;
use Magento\Quote\Api\ShippingMethodManagementInterface as ShippingMethodManager;
use Magento\Quote\Model\Cart\Totals\Item;
use Magento\Quote\Model\Quote\ItemFactory;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Stemcell\Customer\Api\SalespersonRepositoryInterface;
use Stemcell\Customer\Model\LabRepository;
class DefaultConfigProvider extends \Magento\Checkout\Model\DefaultConfigProvider
{
/**
* @var CheckoutSession
*/
protected $checkoutSession;
private ItemFactory $quoteItem;
private SalespersonRepositoryInterface $salespersonRepository;
private LabRepository $labRepository;
public function __construct(
CheckoutHelper $checkoutHelper,
CheckoutSession $checkoutSession,
CustomerRepository $customerRepository,
CustomerSession $customerSession,
CustomerUrlManager $customerUrlManager,
HttpContext $httpContext,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
QuoteItemRepository $quoteItemRepository,
ShippingMethodManager $shippingMethodManager,
ConfigurationPool $configurationPool,
QuoteIdMaskFactory $quoteIdMaskFactory,
LocaleFormat $localeFormat,
\Magento\Customer\Model\Address\Mapper $addressMapper,
\Magento\Customer\Model\Address\Config $addressConfig,
FormKey $formKey,
\Magento\Catalog\Helper\Image $imageHelper,
\Magento\Framework\View\ConfigInterface $viewConfig,
\Magento\Directory\Model\Country\Postcode\ConfigInterface $postCodesConfig,
Cart\ImageProvider $imageProvider,
\Magento\Directory\Helper\Data $directoryHelper,
CartTotalRepositoryInterface $cartTotalRepository,
ScopeConfigInterface $scopeConfig,
\Magento\Shipping\Model\Config $shippingMethodConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
UrlInterface $urlBuilder,
CaptchaConfigPostProcessorInterface $configPostProcessor,
ItemFactory $quoteItem,
SalespersonRepositoryInterface $salespersonRepository,
LabRepository $labRepository,
AddressMetadataInterface $addressMetadata = null,
AttributeOptionManagementInterface $attributeOptionManager = null,
CustomerAddressDataProvider $customerAddressData = null,
Escaper $escaper = null
)
{
parent::__construct($checkoutHelper, $checkoutSession, $customerRepository, $customerSession, $customerUrlManager, $httpContext, $quoteRepository, $quoteItemRepository, $shippingMethodManager, $configurationPool, $quoteIdMaskFactory, $localeFormat, $addressMapper, $addressConfig, $formKey, $imageHelper, $viewConfig, $postCodesConfig, $imageProvider, $directoryHelper, $cartTotalRepository, $scopeConfig, $shippingMethodConfig, $storeManager, $paymentMethodManagement, $urlBuilder, $configPostProcessor, $addressMetadata, $attributeOptionManager, $customerAddressData, $escaper);
$this->checkoutSession = $checkoutSession;
$this->quoteItem = $quoteItem;
$this->salespersonRepository = $salespersonRepository;
$this->labRepository = $labRepository;
}
protected function addAdditionalItemsData(Item $item)
{
$data = [];
$quoteItem = $this->quoteItem->create()->load($item->getItemId());
$data['sku'] = $quoteItem->getSku();
if ($quoteItem->getSalesPersonId()) {
$salesperson = $this->salespersonRepository->get($quoteItem->getSalesPersonId());
$data['salesperson_name'] = $salesperson->getName();
}
if ($quoteItem->getLabCode()) {
$lab = $this->labRepository->getByLabCode($quoteItem->getLabCode());
$data['booking_account'] = $lab->getBookingAccount();
}
$data['cd_code'] = $quoteItem->getCd();
$data['quote_code'] = $quoteItem->getData('quote');
$data['pi_sap_id'] = $quoteItem->getData('pi_sap_id');
return $data;
}
protected function getTotalsData()
{
/** @var \Magento\Quote\Api\Data\TotalsInterface $totals */
$totals = $this->cartTotalRepository->get($this->checkoutSession->getQuote()->getId());
$items = [];
/** @var Item $item */
foreach ($totals->getItems() as $item) {
$items[] = array_merge($item->__toArray(), $this->addAdditionalItemsData($item));
}
$totalSegmentsData = [];
/** @var \Magento\Quote\Model\Cart\TotalSegment $totalSegment */
foreach ($totals->getTotalSegments() as $totalSegment) {
$totalSegmentArray = $totalSegment->toArray();
if (is_object($totalSegment->getExtensionAttributes())) {
$totalSegmentArray['extension_attributes'] = $totalSegment->getExtensionAttributes()->__toArray();
}
$totalSegmentsData[] = $totalSegmentArray;
}
$totals->setItems($items);
$totals->setTotalSegments($totalSegmentsData);
$totalsArray = $totals->toArray();
if (is_object($totals->getExtensionAttributes())) {
$totalsArray['extension_attributes'] = $totals->getExtensionAttributes()->__toArray();
}
return $totalsArray;
}
}