Back to Top

List All Taxes in Magento 2

Updated 28 December 2022

In this dev doc article, we will learn how to list all taxes in Magento 2 platform. Magento manages taxes for catalog, shipping and customers as well.

There have several changes between Magento and Magento 2 in the tax structure. So this article demonstrates how to fetch all taxes in Magento 2.

Use below code to retrieve all taxes,

<?php
 
namespace Webkul\Demo\Model;

use \Magento\Tax\Model\Calculation\Rate;
 
class Taxrate extends \Magento\Framework\DataObject 
    implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * @var Rate
     */
    protected $_taxModelConfig;
     
    /**
     * @param Rate               $taxModelConfig
     */
    public function __construct(
        Rate $taxModelConfig
    ) {
        $this->_taxModelConfig = $taxModelConfig;
    }
  
    public function toOptionArray()
    {
        $taxRates = $this->_taxModelConfig->->getCollection()->getData();
        $taxArray = array();
        foreach ($taxRates as $tax) {
            $taxRateId = $tax['tax_calculation_rate_id'];
            $taxCode = $tax["code"];
            $taxRate = $tax["rate"];
            $taxName = $taxCode.'('.$taxRate.'%)';
            $taxArray[$taxRateId] = $taxName;
        }
        return $taxArray;
    }
}
  • _taxModelConfig: Is an object of “Magento\Tax\Model\Calculation\Rate” class.
  • $tax[“code”] : It will give tax code.
  • $tax[“rate”] : It will give tax rate.
  • $tax[“tax_calculation_rate_id”] : It will give tax id.
  • toOptionArray() : This method will return a list of taxes with tax rates.

Thank you for reading this dev doc on how to list and fetch taxes in Magento 2, we hope you liked it. For more queries, please reach out to our team via a support ticket.

Searching for an experienced
Magento 2 Company ?
Find out More

You may also find a wide range of Magento 2 extensions including Magento 2 GST and Magento 2 Avalara Tax, helping you manage and automate complex tax calculations easily.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


1 comments

  • Ninad Nagwekar
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home