2

In Magento 2 how we can check from order object that what is the settings for the tax either its including or excluding. We can get the tax info generally by this function

$this->scopeConfig->getValue('tax/calculation/price_includes_tax',
                $storeScope);

but this function return the current setting for the tax, is there any method to check the setting from the order object ? So we can get the tax settings while the order created.

What if we save the settings while order creation ?

1

6 Answers 6

1

You must extend the Magento\Sales\Model\Order\Tax\Item class and add an extension attribute, like tax_actual_setting, which will store an actual value of the corresponding tax type setting. There is two types of tax items by default - product and shipping. Each one has own calculation setting in the store config. Based on that value you could determine in future which setting has been used. Anyway detect that value on the store without additional field & code is unavailable, because the Magento is not saving this setting out of the box (with an order).

The tables you may be interested in is:

  • sales_order_tax_item
  • sales_order_tax

Classes:

  • Magento\Sales\Model\Order\Tax
  • Magento\Sales\Model\Order\Tax\Item
  • Magento\Sales\Model\ResourceModel\Order\Tax
  • Magento\Sales\Model\ResourceModel\Order\Tax\Item

The data is linked with an order using order_id field in the sales_order_tax table.

2
+25

Tax rate is not based on order, but on order item, so you will need to check these:

$orderItem->getTaxPercent()

  • For it you have to load orderitem object for it.

For invoice items you need to request the associated order item by calling

$invoiceItem->getOrderItem()->getTaxPercent()

  • For it you have to load orderitem object for it..

To get the whole tax information of an order, you may use

$order->getFullTaxInfo();

, which returns the whole tax calculation result.

0

In the order object there is a method called getBaseSubtotalInclTax() . Can you check with this method and try to compare with default basetotal.

2
  • which default basetotal method ?
    – Mehar
    Commented Jan 1, 2020 at 15:26
  • In order object. Commented Jan 4, 2020 at 16:41
0

I know that the use of objectManager is not a good idea but this answer is for understanding If this is helpful then I'm going to post proper answer with correct dependency..!

Please check the below answer and let me know if it's helpful.

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$ordersObj = $objectManager->create('Magento\Sales\Model\ResourceModel\Order\CollectionFactory')->create();
foreach($ordersObj as $orders)
{ 
 $incrementId = $orders->getData("increment_id");   
 $orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($incrementId);
 $orderItems = $orderData->getAllItems();

  foreach ($orderItems as $item) {
   if($item->getTaxAmount() != ''){ // Here you can get all order data
      //Tax is applied
   }
   else
   {
    //Tax is not applied  
   }
  }
}

?>
2
  • $item->getTaxAmount() also return the value in case of exclusive tax settgins.
    – Mehar
    Commented Jan 7, 2020 at 10:43
  • @Mehar, No, it will not return the value in case of exclusive tax setting, If it will return value then error in your Tax Setting from admin side. Commented Jan 7, 2020 at 10:59
0

Something like this?

    public function pricesIncludeTax($order) {
        $store = $order->getStore();
        $pricesIncludeTax = $this->scopeConfig->
        getValue('tax/calculation/price_includes_tax', 
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE, 
            $store->getCode()
        );
        return ($pricesIncludeTax == 1); 
    }

That won't help you if you've changed the tax settings from inc to ex since the order was placed, but it will give you the price display settings for the store applicable to the order.

-1

You can use below method to get full tax information about tax from order object

$order->getFullTaxInfo()

go get tax percent first you need to fetch order's items collection and then you can get tax percent using below method

$orderItem->getTaxPercent()

1
  • $order->getFullTaxInfo() not returning any information.
    – Mehar
    Commented Jan 5, 2020 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.