1

I have used below code to get tax class name from id.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$taxClassObj = $objectManager->create('Magento\Tax\Model\TaxClass\Source \Product');

$taxid = 2;
echo $taxName =  $taxClassObj->getOptionText($taxid);

This returns the Options text from id that is "Taxable Goods"

How to get the Id from Text?

Can we get Tax class id(value) from the tax class name?

Can anyone look into this and update your thoughts. thanks

7
  • you should have all information in $taxClassess, that's an array with label / value, while label is the taxClassId and label the name
    – HelgeB
    Feb 14, 2019 at 12:24
  • @HelgeB, yes I have all options in that variable, but can we get the id from a text(class name)? Feb 14, 2019 at 12:26
  • 1
    Did you try something like this? foreach ($taxClassess as $taxclass){ if ($taxclass['label'] == 'class name') $myTaxId = $taxclass['value']; }
    – HelgeB
    Feb 14, 2019 at 12:29
  • i was stuck with this can u plz help me to solve it @HelgeB Feb 14, 2019 at 12:30
  • @HelgeB, yes that will work, but is there any default functions to get from repository or something other way? Feb 14, 2019 at 12:32

1 Answer 1

1

I use this in a plugin but shoud work in every class:

<?php
namespace Vendor\Module\Plugin;

use Magento\Tax\Api\Data\TaxClassKeyInterface;

class PluginName
{
    ....

    public function __construct(
        \Magento\Tax\Api\TaxClassManagementInterface $taxClassManagementInterface,
        \Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyDataObjectFactory,
        \Psr\Log\LoggerInterface $logger
    )
    {
        $this->taxClassManagementInterface = $taxClassManagementInterface;
        $this->taxClassKeyDataObjectFactory = $taxClassKeyDataObjectFactory;
        $this->logger = $logger;
    }

    ....

    public function getTaxClassId($clasName){
        $taxClassId = $this->taxClassManagementInterface->getTaxClassId(
            $this->taxClassKeyDataObjectFactory->create()
                ->setType(TaxClassKeyInterface::TYPE_NAME)
                ->setValue($clasName)
        );
        return $taxClassId;
    }

    ....
}
1
  • in Magento 2.4.4. use use Magento\Tax\Model\TaxClass\KeyFactory as TaxClassKeyInterfaceFactory; Jan 2, 2023 at 13:45

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.