6

I am trying to convert below code in magento2. I'm unable to find equivalent class of Mage::getModel('tax/calculation') in Magento2.

$taxCalculation = Mage::getModel('tax/calculation'); $request = $taxCalculation->getRateRequest(null, null, null, $store); $taxClassId = $_product->getTaxClassId(); $percent = $taxCalculation->getRate($request->setProductClassId($taxClassId)); 

Help appreciated.

4 Answers 4

3

in your block file

protected $calculation; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Tax\Model\Calculation $calculation, ......... ) { ........... $this->calculation = $calculation; ........... } public function yourmethod() { $taxCalculation = $this->calculation; $request = $taxCalculation->getRateRequest(null, null, null, $store); $taxClassId = $_product->getTaxClassId(); $percent = $taxCalculation->getRate($request->setProductClassId($taxClassId)); } 
4
  • hi,@Muruza, i need to set custom tax price for product, how that can be achieved in magneto2? Commented May 14, 2019 at 10:33
  • @jafarpinjar what do you mean by customTaxPrice can you please explain Commented May 16, 2019 at 2:55
  • I need to set fixed tax amount for the quote item, how that can be achieved? Here is my complete question, magento.stackexchange.com/questions/274520/… Commented May 16, 2019 at 3:47
  • hi @Murtuza, are you cleared now? Commented May 16, 2019 at 6:01
1

Magento 2 has a separate module for tax calculations module-tax. There you can find the equivalent of the Mage_Tax_Model_Calculation class.

<?php namespace Magento\Tax\Model; class Calculation extends \Magento\Framework\Model\AbstractModel { ... 

This class also contains the methods getRateRequest() and getRate().

0

If you are using an block you can create a protected variable:

... protected $_taxCalculation; ... 

And declaret model on construct:

public function __construct( ... \Magento\Tax\Model\TaxCalculation $taxCalculation ...) { ... $this->_taxCalculation = $taxCalculation; } ... 

Now you can use functions of TaxCalculation model from: /vendor/magento/module-tax/Model/TaxCalculation.php

For example:

public function myFunction(){ return $this->_taxCalculation->getRate(1); } 
0
$taxCalculation = Mage::getModel('tax/calculation'); 

equivalent class in Magento 2 Please inject Magento\Tax\Model\TaxCalculation $taxCalculation

getRateRequest,getRate his method is in (Magento\Tax\Model\TaxCalculation) model

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.