Pergunta

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 ?

Foi útil?

Solução

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.

Outras dicas

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.

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

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()

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

?>

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top