Magento2 List All Taxes

Magento manages taxes for catalog, shipping and customers as well. There are some changes in between Magento and Magento 2 for tax structure.

<?php
 
namespace Magemeta\Tax\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 list of taxes with tax rates.

Hope this help you for taxes !

Leave a Reply