I have created custom module, In that I have just override the Mage_Catalog_Block_Product class to get the getPrice(). But it's returning an error Call to a member function getProductId() on a non-object?

my php code is:


class Course_Mca_IndexController extends Mage_Core_Controller_Front_Action
    public function indexAction()

//block file


class Course_Mca_Block_Products extends Mage_Catalog_Block_Product
    public function getPrice()
          return parent::getPrice();

//web.phtml file

<?php echo $this->getPrice() ?>

//layout.xml file

<?xml version="1.0"?>
<layout version="0.1.0">
        <reference name="content">
            <block type="mca/products" name="mca_mca" template="mca/web.phtml"></block>


<?xml version="1.0"?>


<!-- it's not working -->
        <!-- <catalog>
<!-- it's working -->

Can anyone tell me where I went wrong?

Thanks in advance.

  • How did you include the web.phtml file? And what does get_class($this); say? Oct 24 '13 at 6:20
  • in layout.xml file I include the type=block_file and template=.phtml_file Oct 24 '13 at 6:22
  • could you maybe add the code in your answer? Oct 24 '13 at 6:52
  • @AnnaVölkl, see my code is updated Oct 24 '13 at 6:58
  • @AnnaVölkl, see code is updated.looking for my bug where it is? Oct 24 '13 at 7:17

take a look at the original getPrice method.
It looks like this:

public function getPrice()
    return $this->getProduct()->getPrice();

Digging deeper you end up in Mage_Catalog_Block_Product::getProduct() that looks like this:

public function getProduct()
    if (!$this->getData('product') instanceof Mage_Catalog_Model_Product) {
        if ($this->getData('product')->getProductId()) {
            $productId = $this->getData('product')->getProductId();
        if ($productId) {
            $product = Mage::getModel('catalog/product')->load($productId);
            if ($product) {
    return $this->getData('product');

In your case $this->getData('product') return null, and since null is not an instance of Mage_Catalog_Model_Product you enter the first if statement and it crashes on this line: if ($this->getData('product')->getProductId()) { because you cannot call null->getProductId().

Any instance of Mage_Catalog_Block_Product makes sense if you attach a product to it. Considering your layout file, I can conclude that you are trying to use your block without any product.

Here is how your Mca_IndexController::indexAction() should look:

$block = $this->getLayout()->getBlock('mca_mca');
if ($block){
    $productId = 15;//for example
    $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);
  • I do the leg work you get the answer ;) Oct 24 '13 at 7:37
  • 1
    @DavidManners. Well...that happens. I've ran a few marathons myself in here just to finish second :).
    – Marius
    Oct 24 '13 at 7:40
  • @Marius, thank you working fine,but actually I override this in config.xml file given <catalog> <rewrite> <product>Course_Mca_Block_Products</product> </rewrite> </catalog> this code it's not working.if I give <mca> <class>Course_Mca_Block</class> </mca> this code it's working. which one is correct? Oct 24 '13 at 9:19
  • @Marius, which one is correct? see in block section I have updated in my post. Oct 24 '13 at 9:21
  • @ManojKumar.Well...obviously the one that works is correct
    – Marius
    Oct 24 '13 at 10:05

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.