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.

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));
 }
|improve this answer|||||
  • hi,@Muruza, i need to set custom tax price for product, how that can be achieved in magneto2? – jafar pinjar May 14 '19 at 10:33
  • @jafarpinjar what do you mean by customTaxPrice can you please explain – Murtuza Zabuawala May 16 '19 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/… – jafar pinjar May 16 '19 at 3:47
  • hi @Murtuza, are you cleared now? – jafar pinjar May 16 '19 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().

|improve this answer|||||
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);
}
|improve this answer|||||
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

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.