From 665b82805026f91e65eb59bbaff51546c9df15f5 Mon Sep 17 00:00:00 2001 From: MykolaMalovanets Date: Wed, 16 Aug 2023 13:38:21 +0300 Subject: [PATCH] M2 - fix tax details issue for the simple products. --- Model/Quote/GetQuote.php | 1 + Model/Quote/Result/Builder.php | 3 +- .../Quote/AddAppliedTaxToQuoteObserver.php | 36 ++++++++++--------- etc/webapi_rest/events.xml | 4 +-- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Model/Quote/GetQuote.php b/Model/Quote/GetQuote.php index bc43b33..9f2c4e2 100644 --- a/Model/Quote/GetQuote.php +++ b/Model/Quote/GetQuote.php @@ -67,6 +67,7 @@ public function getQuote( } catch (LocalizedException $e) { return $this->quoteResultBuilder->createErrorResult($e->getMessage()); } + $quote->collectTotals(); $this->cart->setQuote($quote); return $this->quoteResultBuilder->createSuccessResult($quote); } diff --git a/Model/Quote/Result/Builder.php b/Model/Quote/Result/Builder.php index 6632fc6..f3c13c8 100644 --- a/Model/Quote/Result/Builder.php +++ b/Model/Quote/Result/Builder.php @@ -124,10 +124,9 @@ private function processQuoteItems(CartInterface $quote): void continue; } $parentProduct = null; - if ($item->getParentItem() !== null) { + if ($item->getParentItem()) { $parentItem = $item->getParentItem(); $item->getExtensionAttributes()->setParentItemId($parentItem->getId()); - $item->getExtensionAttributes()->setTaxDetails($parentItem->getExtensionAttributes()->getTaxDetails()); $item->setQty($parentItem->getQty()); $item->setPrice($parentItem->getPrice()); $parentProduct = $parentItem->getProduct(); diff --git a/Observer/Quote/AddAppliedTaxToQuoteObserver.php b/Observer/Quote/AddAppliedTaxToQuoteObserver.php index 262e0fd..884f8db 100644 --- a/Observer/Quote/AddAppliedTaxToQuoteObserver.php +++ b/Observer/Quote/AddAppliedTaxToQuoteObserver.php @@ -47,12 +47,10 @@ public function __construct( public function execute(Observer $observer) { $quote = $observer->getEvent()->getQuote(); - $shippingAddress = $quote->getShippingAddress(); foreach ($quote->getAllItems() as $item) { - if ($shippingAddress->getAppliedTaxes()) { - $this->addAppliedTaxesToItem($shippingAddress->getAppliedTaxes(), $item); - } + $this->addAppliedTaxesToItem($item); } + $shippingAddress = $quote->getShippingAddress(); $quote->getExtensionAttributes()->setShippingTaxAmount($shippingAddress->getShippingTaxAmount()); $quote->getExtensionAttributes()->setBaseShippingTaxAmount($shippingAddress->getBaseShippingTaxAmount()); } @@ -60,25 +58,31 @@ public function execute(Observer $observer) /** * Populate cart item with applied taxes. * - * @param array $appliedTaxes * @param CartItemInterface $item * @return void */ - private function addAppliedTaxesToItem(array $appliedTaxes, CartItemInterface $item): void + private function addAppliedTaxesToItem(CartItemInterface $item): void { - $taxDetails = []; - foreach ($appliedTaxes as $appliedTaxData) { - if ((int)$item->getItemId() !== (int)$appliedTaxData['item_id']) { - continue; - } - $appliedTax = $this->appliedTaxFactory->create(); + if ($item->getChildren()) { + return; + } + $origItem = $item; + if ($item->getParentItem()) { + $item = $item->getParentItem(); + } + if (!$item->getAppliedTaxes()) { + return; + } + $itemTaxDetails = []; + foreach ($item->getAppliedTaxes() as $tax) { + $itemAppliedTax = $this->appliedTaxFactory->create(); $this->objectHelper->populateWithArray( - $appliedTax, - $appliedTaxData, + $itemAppliedTax, + $tax, AppliedTaxInterface::class ); - $taxDetails[] = $appliedTax; + $itemTaxDetails[] = $itemAppliedTax; } - $item->getExtensionAttributes()->setTaxDetails($taxDetails); + $origItem->getExtensionAttributes()->setTaxDetails($itemTaxDetails); } } diff --git a/etc/webapi_rest/events.xml b/etc/webapi_rest/events.xml index 21b406a..73a23e4 100644 --- a/etc/webapi_rest/events.xml +++ b/etc/webapi_rest/events.xml @@ -1,6 +1,6 @@ - - + +