15

Update: I found that the class I actually need to rewrite is SMDesign_SMDZoom_Block_Product_View_Media and not SMDesign_ColorswatchProductView_Block_Product_View_Media like I originally thought. However, the exact same issue still applies.

The SMDesign SMDZoom extension rewrites block class 'catalog/product_view_media'. The xml for that looks like this:

       <global>
    ...         
       <blocks>
        <smdzoom>
            <class>SMDesign_SMDZoom_Block</class>
        </smdzoom>
    </blocks>
    <blocks>
        <catalog>
            <rewrite>
                <product_view_media>SMDesign_SMDZoom_Block_Product_View_Media</product_view_media>
            </rewrite>
        </catalog>
    </blocks>
           ....
       </global>

I want to rewrite this class again. I want to rewrite the catalog/product_view_media class that this smdzoom module is rewriting. Can this be done? What would the config xml look like in my module to do this?

Currently this is my module init xml file:

<config>
    <modules>
        <Goorin_SMDZoom>
            <active>true</active>
            <codePool>local</codePool>
        </Goorin_SMDZoom>
        <depends>
            <Mage_Catalog />
            <SMDesign_SMDZoom />
        </depends>
    </modules>
</config>

and the module config.xml file:

<global>
        <blocks>
            <gbismdzoom>
                <class>Goorin_SMDZoom_Block</class>
            </gbismdzoom>
            <catalog>
                <rewrite>
                    <product_view_media>Goorin_SMDZoom_Block_Product_View_Media</product_view_media>
                </rewrite>
            </catalog>
        </blocks>
    </global>

My new class simply looks like this for testing:

class Goorin_SMDZoom_Block_Product_View_Media extends SMDesign_SMDZoom_Block_Product_View_Media
{
    public function _construct() {
        parent::_construct();
    }
}

I'm testing this by displaying template path hints, and I'm seeing SMDesign block still being used on the product view page. What am I doing wrong?

3 Answers 3

13

I put the <depends> node in the wrong location in my module's init xml file. It must be nested within the module name's node, like so:

<config>
    <modules>
        <Goorin_SMDZoom>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
                <SMDesign_SMDZoom />
            </depends>
        </Goorin_SMDZoom>
    </modules>
</config>
10

This part here:

        <colorswatchproductview>
            <rewrite>
                <product_view_media>Goorin_ColorswatchProductView_Block_Product_View_Media</product_view_media>
            </rewrite>
        </colorswatchproductview>

will only help you if the original instantiation is done via createBlock('colorswatchproductview/product_view_media)
this is unlikely since the first extension is using a rewrite,ie they want to be shown anywhere where
createBlock('catalog/product_view_media') is used

One option would be to use a rewrite for the same block

  <catalog>
    <rewrite>
      <product_view_media>Goorin_ColorswatchProductView_Block_Product_View_Media</product_view_media>
    </rewrite>
  </catalog>

and then in your block class extend the other extension block

class Goorin_ColorswatchProductView_Block_Product_View_Media extends SMDesign_ColorswatchProductView_Block_Product_View_Media

For this to work your extension needs to be loaded after the first one. During my tests here http://magebase.com/magento-tutorials/magento-extension-clashes-winners-and-loosers/ I worked out that the load order is based on alphabetical order of your app/etc/modules/ xml files. Alternatively this order can be influenced by using a Depends node (SMDesign_ColorswatchProductView).

You are seeing the error message since you are trying to render the block in isolation but it needs a product, which it loads from its parent block:

abstract class Mage_Catalog_Block_Product_View_Abstract extends Mage_Catalog_Block_Product_Abstract
{
    /**
     * Retrive product
     *
     * @return Mage_Catalog_Model_Product
     */
    public function getProduct()
    {
        $product = parent::getProduct();
        if (is_null($product->getTypeInstance(true)->getStoreFilter($product))) {
            $product->getTypeInstance(true)->setStoreFilter(Mage::app()->getStore(), $product);
        }

        return $product;
    }
4
  • I went off your instructions and can't rewrite the class, even with the depends node. Jun 26, 2013 at 16:29
  • Try renaming your module init xml file to z.xml followed by a cache refresh. Jun 26, 2013 at 20:49
  • Sorry I haven't gotten back to this in awhile. I renamed the init xml file to zzz.xml and the rewrite worked! So I guess I need to figure out why the <depends> node isn't working.. Sep 11, 2013 at 17:27
  • I +1 thanks for the help and leading me to the answer :) Sep 11, 2013 at 17:47
3

This is why re-writing core classes sucks.

Shooting from there hip here but I think your best bet would be to extend the color swatch implementation of Mage_Catalog_Blog_Product_View_Media and then make your module depend upon the color swatch module.

Alan Storm answered a similar question here.

Even better would be to try and do whatever you're trying to do through an observer.

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.