1

Im trying to retrieve the Current Product ID inside the Tax-model-Calculation.php (code in -> https://github.com/adam-paterson/magento-ce-1.9.0.1/blob/master/app/code/core/Mage/Tax/Model/Calculation.php )

public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true)
    {
        $taxRate = $taxRate / 100;
        if ($priceIncludeTax) {
            $amount = $price * (1 - 1 / (1 + $taxRate));
        } else {
            $amount = $price * $taxRate;
        }
        if ($round) {
            return $this->round($amount);
        }
        return $amount;
    }

I tried the following code:

public function calcTaxAmount($price, $taxRate, $priceIncludeTax=false, $round=true)
{
    $taxRate = $taxRate/100;
    $product = Mage::registry('current_product')->getId();
    var_dump($product);

but the following information is displayed in frontpage when I debug using var_dump($product):

Fatal error: Uncaught Error: Call to a member function getId() on null

Additionally I tried $product = $this->getProduct()->getId();

with same result.

  • 1
    are you use tax cal model on product list page or product view page ? – Abdul Mar 14 '16 at 6:46
  • hi @Abdul , calctaxamount is used in home, productlist, view and cart. thats the reason why I want to use current_product id inside the model. – s_h Mar 14 '16 at 8:07
  • 1
    If your need current product id in calcTaxAmount function then need to add one more argument in above function. If your wand then i will explain in details – Abdul Mar 14 '16 at 10:06
  • hi @Abdul , yes please. Would be great!, thanks. – s_h Mar 14 '16 at 16:41
  • 1
    check ans and let me know if any queries :) – Abdul Mar 15 '16 at 6:51
2
+100

Follow bellow steps:

Step : 1 First copy core file from app/code/core/Mage/Tax/Helper/Data.php to local app/code/local/Mage/Tax/Helper/Data.php or override helper data

After replace bellow functions

public function getPrice($product, $price, $includingTax = null, $shippingAddress = null, $billingAddress = null,
                         $ctc = null, $store = null, $priceIncludesTax = null, $roundPrice = true)
{
    if (!$price) {
        return $price;
    }
    $store = $this->_app->getStore($store);
    if (!$this->needPriceConversion($store)) {
        return $store->roundPrice($price);
    }
    if (is_null($priceIncludesTax)) {
        $priceIncludesTax = $this->priceIncludesTax($store);
    }

    $percent = $product->getTaxPercent();
    $includingPercent = null;

    $taxClassId = $product->getTaxClassId();
    if (is_null($percent)) {
        if ($taxClassId) {
            $request = Mage::getSingleton('tax/calculation')
                ->getRateRequest($shippingAddress, $billingAddress, $ctc, $store);
            $percent = Mage::getSingleton('tax/calculation')
                ->getRate($request->setProductClassId($taxClassId));
        }
    }
    if ($taxClassId && $priceIncludesTax) {
        if ($this->isCrossBorderTradeEnabled($store)) {
            $includingPercent = $percent;
        } else {
            $request = Mage::getSingleton('tax/calculation')->getRateOriginRequest($store);
            $includingPercent = Mage::getSingleton('tax/calculation')
                ->getRate($request->setProductClassId($taxClassId));
        }
    }

    if ($percent === false || is_null($percent)) {
        if ($priceIncludesTax && !$includingPercent) {
            return $price;
        }
    }

    $product->setTaxPercent($percent);
    if ($product->getAppliedRates() == null) {
        $request = Mage::getSingleton('tax/calculation')
                ->getRateRequest($shippingAddress, $billingAddress, $ctc, $store);
        $request->setProductClassId($taxClassId);
        $appliedRates =  Mage::getSingleton('tax/calculation')->getAppliedRates($request);
        $product->setAppliedRates($appliedRates);
    }

    if (!is_null($includingTax)) {
        if ($priceIncludesTax) {
            if ($includingTax) {
                /**
                 * Recalculate price include tax in case of different rates.  Otherwise price remains the same.
                 */
                if ($includingPercent != $percent) {
                    // determine the customer's price that includes tax
                    $price = $this->_calculatePriceInclTax($price, $includingPercent, $percent, $store, $product);
                }
            } else {
                $price = $this->_calculatePrice($price, $includingPercent, false, false, $product);
            }
        } else {
            if ($includingTax) {
                $appliedRates = $product->getAppliedRates();
                if (count($appliedRates) > 1) {
                    $price = $this->_calculatePriceInclTaxWithMultipleRates($price, $appliedRates);
                } else {
                    $price = $this->_calculatePrice($price, $percent, true, false, $product);
                }
            }
        }
    } else {
        if ($priceIncludesTax) {
            switch ($this->getPriceDisplayType($store)) {
                case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
                case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
                    if ($includingPercent != $percent) {
                        // determine the customer's price that includes tax
                        $taxablePrice = $this->_calculatePriceInclTax($price, $includingPercent, $percent, $store, $product);
                        // determine the customer's tax amount,
                        // round tax unless $roundPrice is set explicitly to false
                        $tax = $this->getCalculator()->calcTaxAmount($taxablePrice, $percent, true, $roundPrice, $product);
                        // determine the customer's price without taxes
                        $price = $taxablePrice - $tax;
                    } else {
                        //round tax first unless $roundPrice is set to false explicitly
                        $price = $this->_calculatePrice($price, $includingPercent, false, $roundPrice, $product);
                    }
                    break;

                case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
                    $price = $this->_calculatePrice($price, $includingPercent, false, false, $product);
                    $price = $this->_calculatePrice($price, $percent, true, false, $product);
                    break;
            }
        } else {
            switch ($this->getPriceDisplayType($store)) {
                case Mage_Tax_Model_Config::DISPLAY_TYPE_INCLUDING_TAX:
                    $appliedRates = $product->getAppliedRates();
                    if (count($appliedRates) > 1) {
                        $price = $this->_calculatePriceInclTaxWithMultipleRates($price, $appliedRates);
                    } else {
                        $price = $this->_calculatePrice($price, $percent, true, false, product);
                    }
                    break;

                case Mage_Tax_Model_Config::DISPLAY_TYPE_BOTH:
                case Mage_Tax_Model_Config::DISPLAY_TYPE_EXCLUDING_TAX:
                    break;
            }
        }
    }
    if ($roundPrice) {
        return $store->roundPrice($price);
    } else {
        return $price;
    }
}

''

protected function _calculatePriceInclTax($storePriceInclTax, $storePercent, $customerPercent, $store, $product=false)
{
    $priceExclTax         = $this->_calculatePrice($storePriceInclTax, $storePercent, false, false);
    $customerTax          = $this->getCalculator()->calcTaxAmount($priceExclTax, $customerPercent, false, false, $product);
    $customerPriceInclTax = $store->roundPrice($priceExclTax + $customerTax);
    return $customerPriceInclTax;
}

''

protected function _calculatePrice($price, $percent, $type, $roundTaxFirst = false, $product = false)
{
    $calculator = $this->getCalculator();
    if ($type) {
        $taxAmount = $calculator->calcTaxAmount($price, $percent, false, $roundTaxFirst, $product);
        return $price + $taxAmount;
    } else {
        $taxAmount = $calculator->calcTaxAmount($price, $percent, true, $roundTaxFirst, $product);
        return $price - $taxAmount;
    }
}

Step : 2 After copy core file from app/code/core/Mage/Tax/Model/Calculation.php to local app/code/local/Mage/Tax/Model/Calculation.php or override model file Calculation.php

and replace code from :

public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true)
{
    $taxRate = $taxRate / 100;

    if ($priceIncludeTax) {
        $amount = $price * (1 - 1 / (1 + $taxRate));
    } else {
        $amount = $price * $taxRate;
    }

    if ($round) {
        return $this->round($amount);
    }

    return $amount;
}

to

public function calcTaxAmount($price, $taxRate, $priceIncludeTax = false, $round = true, $product = false)
{
    if($product) {
    echo $product->getId();
    }

    $taxRate = $taxRate / 100;

    if ($priceIncludeTax) {
        $amount = $price * (1 - 1 / (1 + $taxRate));
    } else {
        $amount = $price * $taxRate;
    }

    if ($round) {
        return $this->round($amount);
    }

    return $amount;
}
|improve this answer|||||
  • what can I say! amazing. thank you so much. This post currently says "question eligible for bounty in 19 hours", I will not close the question so I can assign a bounty tomorrow and granted to you. once again, thank you!. – s_h Mar 15 '16 at 7:48
0

Try to get it using Request object:

$productId = Mage::app()->getRequest()->getParam('product')

Which gives you requested product id inside calcTaxAmount method.

Hope this will work for you.

|improve this answer|||||
  • hi @jayesh-patel thank you for the msg. I try that but I receive null for $productId = Mage::app()->getRequest()->getParam('product') when I var_dump the value. – s_h Mar 14 '16 at 7:58
0

You can use below method to get current register product id.

  1. Just create a session to set the product id.You need to put below mentioned code in you header.phtml file.

    $productId = Mage::registry('current_product')->getId(); Mage::getSingleton('core/session')->setCurrentProductId($productId);

  2. Now you can get recently created session any where in Magento by using below code.

Mage::getSingleton('core/session')->getCurrentProductId()

Hope it will help you. For more see Alan's answer here.

|improve this answer|||||
  • thanks @arunendra but I receive " Fatal error: Uncaught Error: Call to a member function getId() on null " placing that code in header.phtml – s_h Mar 14 '16 at 8:08
0

Get a current product id in magento 1 by using below lines.

$product = Mage::registry('current_product');
$id = $product->getId();
|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.