Magento: Attribute Set Template

I want to create different product views based on the attribute to which the product belongs: does Magento do a way to do this?

- UPDATE -

Following the suggestion of dan.codes, I added

$update->addHandle('PRODUCT_ATTRIBUTE_SET_ID_'.$product->getAttributeSetId()); 

in Mage_Catalog_ProductController (I duplicated ProductController.php and put it in local / Mage / Catalog / controllers /).

Then I added this to catalog.xml

 <PRODUCT_ATTRIBUTE_SET_ID_9> // PRODUCT ID of Book Attribute Set <label>Catalog Product View (Book)</label> <reference name="product.info"> <block type="catalog/product_view_type_book" name="product.info.book" as="product_type_data" template="catalog/product/view/attribute_set/book.phtml"> <block type="core/text_list" name="product.info.book.extra" as="product_type_data_extra"/> </block> </reference> </PRODUCT_ATTRIBUTE_SET_ID_9> 

right after

 <PRODUCT_TYPE_virtual translate="label" module="catalog"> <label>Catalog Product View (Virtual)</label> <reference name="product.info"> <block type="catalog/product_view_type_virtual" name="product.info.virtual" as="product_type_data" template="catalog/product/view/type/virtual.phtml"> <block type="core/text_list" name="product.info.virtual.extra" as="product_type_data_extra"/> </block> </reference> </PRODUCT_TYPE_virtual> 

Then I created the directory / product / view / attribute _set / book.phtml, but it does not appear on my product view page.

- UPDATE MAGENTO 1.5 -

I noticed that the handler update has moved to the latest version of Magento.

 $update->addHandle('PRODUCT_TYPE_'.$product->getTypeId()); $update->addHandle('PRODUCT_'.$product->getId()); 

located in Mage / Catalog / Helper / Product / View.php now.
I tested and it still works great!

+4
source share
3 answers

No, it’s not, but you can extend the functionality to do this by extending the _initProductLayout method in the Mage_Catalog_ProductController in which this code is located

  $update = $this->getLayout()->getUpdate(); $update->addHandle('default'); $this->addActionLayoutHandles(); $update->addHandle('PRODUCT_TYPE_'.$product->getTypeId()); $update->addHandle('PRODUCT_'.$product->getId()); 

You can add

 $update->addHandle('PRODUCT_ATTRIBUTE_SET_ID_'.$product->getAttributeSetId()); 

Then in your layout.xml you could

 <PRODUCT_ATTRIBUTE_SET_ID_IDHERE> <reference name="root"> <action method="setTemplate"><template>template/path/here.html</template></action> </reference> </PRODUCT_ATTRIBUTE_SET_ID_IDHERE> 
+4
source

If in case you want to switch view.phtml based on a set of attributes, here is what you need to do:

 <PRODUCT_ATTRIBUTE_SET_ID_9> <label>Catalog Product View (Default)</label> <reference name="product.info"> <action method="setTemplate"><template>catalog/product/custom-view.phtml</template></action> </reference> </PRODUCT_ATTRIBUTE_SET_ID_9> 

Just add this to your catalog.xml or local.xml file
Hope this helps. Thanks

+2
source

There is a good tutorial on this subject: http://magebase.com/magento-tutorials/creating-custom-layout-handles/

In this case, the following event is used: controller_action_layout_load_before

To do this, I configured the following in config.xml

 <events> <controller_action_layout_load_before> <observers> <mymodule> <class>mymodule/observer</class> <method>addAttributeSetHandle</method> </mymodule> </observers> </controller_action_layout_load_before> </events> 

And in Observer.php I will have

 public function addAttributeSetHandle(Varien_Event_Observer $observer) { $product = Mage::registry('current_product'); /** * Return if it is not product page */ if (!$this->isBookProduct($product)) { return; } $niceName = 'book'; /* @var $update Mage_Core_Model_Layout_Update */ $update = $observer ->getEvent() ->getLayout() ->getUpdate(); $handles = $update->getHandles(); // Store all handles in a variable $update->resetHandles(); // Remove all handles /** * Rearrange layout handles to ensure PRODUCT_<product_id> * handle is added last */ foreach ($handles as $handle) { $update->addHandle($handle); if ($handle == 'PRODUCT_TYPE_' . $product->getTypeId()) { $update->addHandle('PRODUCT_ATTRIBUTE_SET_' . $niceName); } } } protected function isBookProduct($product) { if (null === $product || !($product instanceof Mage_Catalog_Model_Product)) { return false; } // TODO instead of hardcoded value we could use here something neat to get by name thru eav/entity_attribute_set model, some config value which hold that ID or use some other approach... $book_set_id = 9; if ($product->getAttributeSetId() != $book_set_id) { return false; } return true; } 

This makes it possible to use the following in the xml layout:

  <?xml version="1.0"?> <layout version="0.1.0"> <PRODUCT_ATTRIBUTE_SET_book> <reference name="product.info"> <action method="setTemplate"> <template>mymodule/book/product/view.phtml</template> </action> </reference> </PRODUCT_ATTRIBUTE_SET_book> </layout> 
+2
source

Source: https://habr.com/ru/post/1336138/


All Articles