3

I want to extend pring a function into magento where I assemble some product data.

In the end I want to call a function like $this->getAlternateTitle() within Lists and Products.

This is what I have: app/etc/modules/Namespace_Name.xml

    <?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Catalog>
            <active>true</active>
            <codePool>local</codePool>
        </Namespace_Catalog>
    </modules>
</config>

then I'm making the config in app/code/local/Namespace/Catalog/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Catalog>
            <version>0.0.1</version>
        </Namespace_Catalog>
    </modules>
    <global>
        <blocks>
                <class>Namespace_Catalog_Block_Product_List</class>
        </blocks>
    </global>
</config>

And finally defining the function here: app/code/local/Namespace/Module/Block/Product/List.php

<?php

class Namespace_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_List {
    public function makeBlue() {
     echo "Blueberrys are colorful";
    }
}

I am only getting back: Invalid method Mage_Catalog_Block_Product_List::makeBlue

What do I do wrong here? It's my first magento-function so please be kind with me ;)

4
  • The answers below are correct. Depending on your needs, I would like to suggest the use of event observers to avoid block rewrites. It could help avoiding conflicts between modules in case several modules rewrite the same block. Sep 28, 2015 at 14:21
  • I need this in many views. Is there a way to "copy" it automatically to each other block i need it in? Sep 28, 2015 at 16:30
  • Is the function makeBlue the exact same function you need or is that just a sample function. Depending on the function, you may not need to do all this and just use an helper instead. Sep 29, 2015 at 8:52
  • Actually I want a function available EVERYWHERE I touch a product. It is a logic, rendering some Product attributes together Sep 29, 2015 at 9:41

2 Answers 2

4

config.xml

  <global>

    <blocks>

            <catalog>
                <rewrite>
                    <product_list>Namespace_Catalog_Block_Catalog_Product_List</product_list>
                <product_view>Namespace_Catalog_Block_Catalog_Product_View</product_view>
                </rewrite>
            </catalog>

    </blocks>
  </global>

Write class :

app/code/local/Namespace/Catalog/Block/Catalog/Product/List.php

<?php
class Namespace_Catalog_Block_Catalog_Product_List extends Mage_Catalog_Block_Product_List
{
}

app/code/local/Namespace/Catalog/Block/Catalog/Product/view.php

<?php
class Namespace_Catalog_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
{
}
7
  • That works! Thank you. I understood it a lot more now. Sep 28, 2015 at 15:51
  • What do I have to do to have the function written in view and copied in list? Sep 28, 2015 at 15:51
  • you are welcome:)
    – Amit Bera
    Sep 28, 2015 at 15:52
  • yes.. you need write on List.php & View.php
    – Amit Bera
    Sep 28, 2015 at 15:55
  • Amit, is there a way to recall it within the second class or do I have to copy it by hand? Sep 28, 2015 at 15:57
2

You need to use Magento's class rewrite system to extend the Mage_Catalog_Block_Product_List Block. The following code sample should give you a good start on how to do that:

In app/etc/modules/Namespace_Name.xml:

...
<Namespace_Catalog>
   ...
   <depends>
       <!-- This dependency ensures that your module is loaded after the core module that you are extending. -->
       <Mage_Catalog/>
   </depends>
</Namespace_Catalog>
...

In app/code/local/Namespace/Catalog/config.xml:

...
<blocks>
    <catalog>
        <rewrite>
            <product_list>Namespace_Catalog_Block_Product_List</product_list>
        </rewrite>
    </catalog>
</blocks>
....

If you need more details, this tutorial by Tuts+ is a great resource.

0

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.