0

Pardon me for my lack of Magento knowledge in advance. I'm adding custom functionality to a theme like this:

app/code/local/Myvendor/Mymodule/Block/Catalog/Product/View.php:

class Myvendor_Mymodule_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
{
  public function myCustomFunction() {
    // Custom functionality
  }
}

This function is called in a template like this:

app/design/frontend/myvendor_mymodule/default/template/catalog/product/view/options/wrapper.phtml

<?php echo $this->myCustomFunction(); ?>

However, in Xdebug, $this always points to Mage_Catalog_Block_Product_View

What are the cases in which the custom module's block is not recognized, and $this defaults to the Mage block instead?

  • The custom module is active at app/etc/modules/Myvendor_Mymodule.xml
  • The custom block is specified at app/code/local/Myvendor/Mymodule/etc/config.xml: <blocks> <myvendor_mymodule> <class>Myvendor_Mymodule_Block</class> </myvendor_mymodule> </blocks>

  • The public function name does not conflict with any other function names.

  • It's necessary for the template to be located inside the myvendor_mymodule package.

1 Answer 1

0

You need to rewrite Mage_Catalog_Block_Product_View class, then you can use this way

<?php echo $this->myCustomFunction(); ?>
<blocks>
    <myvendor_mymodule>
        <class>Myvendor_Mymodule_Block</class>
    </myvendor_mymodule>
    <catalog>
        <rewrite>
            <product_view>Myvendor_Mymodule_Block_Catalog_Product_View</product_view>
        </rewrite>
    </catalog>
</blocks>

Clear cache.

1
  • Quick answer and fully working solution. Very much appreciated! Apr 3, 2017 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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