Pergunta

I'm experiencing a weird issue: Let's say category A has a custom design. When I open the product via domain.com/category-a/product.html the custom design applies correctly.

But when I open the product through Magento's canonical/Google sitemap url domain.com/product.html the custom design won't apply.

How can I bypass that behaviour? I suppose that's because Magento doesn't know which category's custom design to apply as a product can have more than one category?

Layout structure is:

  • custom design
  • -> default
  • -> category_custom
Foi útil?

Solução

The product page is rendered from Mage_Catalog_Helper_Product_View::prepareAndRender(). This method takes into account the current category by calling Mage_Catalog_Helper_Product_View::initProduct() in this piece of code:

$categoryId = $params->getCategoryId(); 
//...
if ($categoryId) {
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $product->setCategory($category);
    Mage::register('current_category', $category);
} 

So the flow is something like this:

  1. init the product with its options (including category id)
  2. Render the product based on the result of init.

You can try to add some logic in the init phase to specify the category.
At the end if the initProduct method, this event is dispatched.

Mage::dispatchEvent('catalog_controller_product_init', array('product' => $product)); 

You can create an observer on this and set the product category (something similar to the code above) and the rendering part should take into account your category.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top