In the checkout in the country_id dropdown I have 2 countries: FR and BE.
For FR I have a VAT percentage of 20% and for BE I have 21%.
My goal is to update the tax amount in the checkout.
When I switch countries I triggered an ajax request:
$quote = $this->checkoutSession->getQuote();
$quote->getShippingAddress()->setCountryId($countryId);
if ($countryId === 'BE') {
foreach ($quote->getAllItems() as $item) {
// do smth
}
}
$quote->collectTotals()->save();
I also made this observer, listening to this event: sales_quote_collect_totals_before
:
public function execute(Observer $observer)
{
$quote = $observer->getQuote();
$countryId = $quote->getShippingAddress()->getCountryId();
if ((string)$countryId === 'BE'){
foreach ($quote->getAllItems() as $item) {
$product = $item->getProduct();
$product->setTaxClassId(14); //14 is my Product Tax Class ID
}
}
}
My product includes the TAX . Right now, when I switch to BE , I m getting this:
My grand and subtotals contains the product price including tax + the Tax, not sure why ?
Do you have any idea about how to approach this ?
Thanks