2

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

4 Answers 4

2

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);
    }
}
3
  • 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, 2018 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. Jan 9, 2018 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, 2018 at 14:56
1

Without loop you can get tax by following code :

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

It will return tax by order Item.

0

change from:

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

to:

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

I've created a simple service that returns the tax percent by order ID.

<?php

declare(strict_types=1);

namespace Vendor\Module\Service\Order;

use Magento\Framework\App\ResourceConnection;

/**
 * Class GetOrderTaxPercentageService
 */
class GetOrderTaxPercentageService
{
    /**
     * Sales order tax table
     */
    private const SALES_ORDER_TAX_TABLE = 'sales_order_tax';

    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

    /**
     * GetNotScheduledCustomersToExport constructor.
     *
     * @param ResourceConnection $resourceConnection
     */
    public function __construct(
        ResourceConnection $resourceConnection
    ) {
        $this->resourceConnection = $resourceConnection;
    }

    /**
     * Get order tax percent
     *
     * @param int $orderId
     *
     * @return float
     */
    public function execute(int $orderId): float
    {
        $table = $this->resourceConnection->getTableName(self::SALES_ORDER_TAX_TABLE);
        $connection = $this->resourceConnection->getConnection();
        $select = $connection->select();
        $select->from(['order_tax' => $table], ['percent' => 'order_tax.percent']);
        $select->where('order_tax.order_id = ?', $orderId);
        $percent = (float) $connection->fetchOne($select);

        return (float) ($percent ? $percent/100 : 0.0);
    }
}

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.