سؤال

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?

هل كانت مفيدة؟

المحلول

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>

نصائح أخرى

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;
    }

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top