Bạn có thể quan sát sự kiện sales_quote_address_collect_totals_aftervà đạt được nó. Đối với điều này, bạn cần thiết lập một mô-đun và cấu hình một sự kiện. Hãy nói rằng mô-đun của chúng tôi là MStack_Exchange.
Tập tin : app\code\MStack\Exchange\etc\events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_address_collect_totals_after">
<observer name="changeTaxTotal" instance="MStack\Exchange\Observer\ChangeTaxTotal"/>
</event>
</config>
Tập tin : app\code\MStack\Exchange\Observer\ChangeTaxTotal.php
<?php
namespace MStack\Exchange\Observer;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;
class ChangeTaxTotal implements ObserverInterface
{
public $additionalTaxAmt = 2;
public function execute(Observer $observer)
{
/** @var Magento\Quote\Model\Quote\Address\Total */
$total = $observer->getData('total');
//make sure tax value exist
if (count($total->getAppliedTaxes()) > 0) {
$total->addTotalAmount('tax', $this->additionalTaxAmt);
}
return $this;
}
}
Cuộc gọi quan trọng ở đây là : $total->addTotalAmount('tax', $this->additionalTaxAmt);. Điều này sẽ thêm 2vào số tiền thuế hiện có và tôi nghĩ đó là những gì bạn cần trong trường hợp của bạn. Vì vậy, những gì bạn cần làm là, thay thế $this->additionalTaxAmtbằng giá trị bộ đệm thuế của bạn.
Sự kiện này diễn sales_quote_address_collect_totals_afterra ngay sau khi các tính toán tổng thể đã xảy ra và do đó trở thành một nơi hoàn hảo để chơi xung quanh.
Nếu bạn tò mò muốn biết tổng số tính toán này đang diễn ra ở đâu, thì bạn cần xem xét Magento\Quote\Model\Quote\TotalsCollector::collect()và Magento\Quote\Model\Quote\TotalsCollector::collectAddressTotals()phương pháp.
custom amountđến từ đâu