Files
2026-09-15 10:35:56 +08:00

41 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\CustomerVat\Observer;
use Esun\CustomerVat\Model\TaxContextResolver;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote;
class UpdateQuoteVatContext implements ObserverInterface
{
public function __construct(private readonly TaxContextResolver $resolver)
{
}
public function execute(Observer $observer): void
{
$quote = $observer->getEvent()->getQuote();
if (!$quote instanceof Quote || !(int)$quote->getStoreId()) {
return;
}
$address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
$context = $this->resolver->resolve(
(int)$quote->getStoreId(),
(int)$quote->getCustomerId(),
(string)$address->getCountryId()
);
$vat = $context->vatApplication;
$quote->addData([
'esun_vat_id' => $vat ? (int)$vat->getId() : null,
'esun_vat_number' => $vat ? (string)$vat->getData('vat_number') : null,
'esun_vat_country_id' => $vat ? (string)$vat->getData('country_id') : null,
'esun_vat_company_name' => $vat ? (string)$vat->getData('company_name') : null,
'esun_vat_exempt' => $context->exempt ? 1 : 0,
'esun_nominal_tax_rate' => $context->nominalRate,
'esun_tax_mode' => $context->mode,
]);
}
}