Magento 2 cancella programmaticamente tutte le tasse e aggiungi personalizzazioni in Observer
Sto personalizzando il calcolo delle tasse e aggiungendo le tasse personalizzate. Lo sto facendo utilizzando l'osservatore:
La versione di Magento è 2.3.4, il codice riportato di seguito funziona correttamente e aggiunge tariffe personalizzate, ma il mio requisito per aggiungere cancella tutte le tasse aggiuntive prima di aggiungerne una nuova.
<?xml version="1.0" encoding="UTF-8"?>
<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="Vendor\CustomTax\Observer\ChangeTaxTotal"/>
</event>
</config>
e il file dell'osservatore è
<?php
namespace Vendorname\CustomTax\Observer;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;
class ChangeTaxTotal implements ObserverInterface
{
public function execute(Observer $observer) { /** @var Magento\Quote\Model\Quote\Address\Total */ $total = $observer->getData('total'); $subtotal = $total['subtotal']; $customTax = ($subtotal * 9)/100; if (trim($total['shipping_description']) == "Shipping_methode_name") {
------------in this line I want to clear all tax -------
$total->addTotalAmount('tax', $customTax);
}
return $this;
}
}
Desidero cancellare tutte le tasse aggiuntive prima di aggiungere la mia tariffa personalizzata e voglio anche aggiungere il titolo dell'imposta. $ total-> addTotalAmount ('tax', $ customTax);
Se qualcuno ha un'idea per favore condividimi.
Risposte
Si prega di modificare il codice come di seguito e provare
A partire dal:
if (trim($total['shipping_description']) == "Shipping_methode_name") { ------------in this line I want to clear all tax ------- $total->addTotalAmount('tax', $customTax);
}
Per:
if (trim($total['shipping_description']) == "Shipping_methode_name") {
------------in this line I want to clear all tax -------
$total->setTotalAmount('tax', $customTax);
}
Si prega di utilizzare il codice sottostante per modificare il titolo
if (trim($total['shipping_description']) == "Shipping_methode_name") { ------------in this line I want to clear all tax ------- $total->setTotalAmount('tax', $customTax); $infos = $total->getFullInfo(); foreach($infos as $key1 => $info) {
foreach($info['rates'] as $key2 => $rate) { if($rate == 'Tax') {
$infos[$key1]['rates'][$key2]['title'] = 'YOUR CUSTOM TITLE'; } } } $total->setFullInfo($infos);
}