Skip to content

Commit

Permalink
M2 - fix tax details issue for the simple products.
Browse files Browse the repository at this point in the history
  • Loading branch information
MykolaMalovanets committed Aug 16, 2023
1 parent 543fc84 commit 665b828
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
1 change: 1 addition & 0 deletions Model/Quote/GetQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions Model/Quote/Result/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
36 changes: 20 additions & 16 deletions Observer/Quote/AddAppliedTaxToQuoteObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,38 +47,42 @@ 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());
}

/**
* 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);
}
}
4 changes: 2 additions & 2 deletions etc/webapi_rest/events.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_load_after">
<observer name="add_applied_taxes" instance="Bold\Checkout\Observer\Quote\AddAppliedTaxToQuoteObserver"/>
<event name="sales_quote_collect_totals_after">
<observer name="add_applied_taxes_to_items" instance="Bold\Checkout\Observer\Quote\AddAppliedTaxToQuoteObserver"/>
</event>
</config>

0 comments on commit 665b828

Please sign in to comment.