0

I need to get an order item tax percentage

<?php
namespace Company\Module\Plugin;


class myClass
{
    protected $_orderRepository;


    public function __construct(
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    ) {
        $this->_orderRepository = $orderRepository;
    }

    public function processOrder($orderId)
    {
        $order = $this->_orderRepository->get($orderId);
        foreach ($order->getItems() as $item) {
            var_dump($item->getTaxPercent());
        }
    }
}

I always get the value of 0.00 even though I know the percentage is 20.00.

Could anyone advise how I should get the tax percentage

0

change from:

foreach ($order->getItems() as $item) {
        var_dump($item->getTaxPercent());
    }

to:

foreach ($order->getAllVisibleItems() as $item) {
        var_dump($item->getTaxPercent());
    }
0

Without loop you can get tax by following code :

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

It will return tax by order Item.

0

Try this code. It will help you to get Tax Percent amount.

<?php
namespace Vendor\Module\Helper;

use \Magento\Framework\App\Helper\Context;
use \Magento\Sales\Api\Data\OrderInterface;
use \Magento\Sales\Model\ResourceModel\Order\Tax\Item;

class APIHelper extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Sales\Model\ResourceModel\Order\Tax\Item
     */
    protected $taxItem;

    public function __construct(
        Context $context,
        Item $taxItem
    ) {
        parent::__construct($context);
        $this->taxItem = $taxItem;
    }

    public function someMethod(OrderInterface $order)
    {
        $tax_items = $this->taxItem->getTaxItemsByOrderId($order->getId());
        var_dump($tax_items);
    }
}
  • Yea I saw the post in magento.stackexchange.com/questions/202746/… but I was hoping I could just get the percentage without looping around arrays and cross referencing products – Goose84 Jan 9 '18 at 14:49
  • If you want to get Tax percent by each order item then you have to go through loop. In your code you are doing the same thing. – Abhishek Panchal Jan 9 '18 at 14:51
  • 2
    Figured out my issue, my product was a configurable product and I needed to get the parent's tax percentage – Goose84 Jan 9 '18 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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