Magento 2プログラムですべての税金をクリアし、オブザーバーにカスタムを追加

Aug 25 2020

税計算をカスタマイズしてカスタムを追加しています。これはオブザーバーを使用して行っています。

Magentoのバージョンは2.3.4で、以下のコードは正常に機能し、カスタムレートを追加していますが、新しい税を追加する前に、追加されたすべての税をクリアする必要があります。

<?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>

オブザーバーファイルは

<?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;
    }
}

カスタムレートを追加する前に、追加されたすべての税金をクリアし、税のタイトルも追加したいと思います。$ total-> addTotalAmount( 'tax'、$ customTax);

誰かアイデアがあれば教えてください。

回答

2 RiteshSantra Aug 25 2020 at 06:06

以下のようにコードを変更してみてください

から:

if (trim($total['shipping_description']) == "Shipping_methode_name") { ------------in this line I want to clear all tax ------- $total->addTotalAmount('tax', $customTax);
        }

に:

if (trim($total['shipping_description']) == "Shipping_methode_name") {
           ------------in this line I want to clear all tax -------
            $total->setTotalAmount('tax', $customTax);
        }
RiteshSantra Aug 25 2020 at 14:15

タイトルの変更には以下のコードを使用してください

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);
        }