Add a tab on product page based on attribute set
-
12-12-2019 - |
Question
I would like to add a static block on the frontend product page conditionally, based on the attribute set of the product. (e.g. Helmets -> display helmet size chart, which does not apply to other products.)
I have added:
<reference name='product.info.tabs'>
<block type="cms/block" name="helmets_size_chart">
<action method="setBlockId"><block_id>helmets_size_chart</block_id></action>
</block>
</reference>
to the tabs block in local.xml, but that shows the static block on the tabs section on all product pages. Secondly, I've added:
protected function _prepareLayout() {
$product = Mage::registry('current_product');
if (!($setId = $product->getAttributeSetId())) {
$setId = $this->getRequest()->getParam('set', null);
}
if($setId != 12) { // helmets
$this->getLayout()->unsetBlock('helmets_size_chart');
}
}
to app/code/local/Mage/Catalog/Block/Product/View/Tabs.php When viewing the product page, I can verify indeed the code in the setId != 12 block is parsed, but the unsetBlock() call doesn't do anything.
As you may have guessed, this is my first attempt at something programmatically in Magento, and I have no clue if I'm on the right track. Any thoughts welcome!
Thanks
Solution
Create a custom block and extend from Mage_Cms_Block_Block
You can now do something like this:
class Foo_Bar_Block_Producttab extends Mage_Cms_Block_Block
{
protected function _toHtml()
{
$product = Mage::registry('current_product');
if ($product->getAttributeSetId() == 12) {
$this->setBlockId('helmets_size_chart');
}
return parent::_toHtml();
}
}
And adjust your layout update file:
<reference name='product.info.tabs'>
<block type="foo_bar/producttab" name="helmets_size_chart" />
</reference>
OTHER TIPS
I was going to edit my post with the following info because it didn't work, but I figured it out; my naming convention was wrong. Below the working code:
I've now created that new module. /app/code/local/Momi/Customproducttabs/Block/Producttab.php
class Momi_Customproducttabs_Block_Producttab extends Mage_Cms_Block_Block {
protected function _toHtml() {
$product = Mage::registry('current_product');
if ($product->getAttributeSetId() == 12) {
$this->setBlockId('helmets_size_chart');
}
return parent::_toHtml();
}
}
In my theme's local.xml:
<reference name='product.info.tabs'>
<block type="momi_customproducttabs/producttab" name="helmets_size_chart" />
</reference>
dvdmierden,you can not do this your code. Because of there have a logic which is included the tab according to its format
.
if you want to then you need to customization.
Step1:
first create an module which rewrite
Magento core class Mage_Catalog_Block_Product_View_Tabs
Step2:
Here add a function addCmsBlock()
which is add a static block
to your tab.
According magento default tab is add by addTab()
and render tabs by functon getTabs()
and it renturn
i have have create a custom function and using this function you can add cms block add attibute set a
<?php
class Amit_Custommodule_Block_Product_View_Tabs extends Mage_Catalog_Block_Product_View_Tabs{
function addCmsBlock($alias='amit', $title='Test', $cmsblock='',$attributeset=''){
if (!$title || !$alias || !$cmsblock ||!$attributeset) {
return false;
}
// get product
$product = Mage::registry('current_product');
// check attribute set and not match to return false
if($product->getAttributeSetId()!=$attributeset){
return false;
}
$this->_tabs[] = array(
'alias' => $alias,
'title' => $title
);
$this->setChild($alias,$this->getLayout()->createBlock('cms/block')
->setBlockId($cmsblock));
}
}
NOte: I have rewrite class Mage_Catalog_Block_Product_View_Tabs
In this function alias name,title and cms Identifier
and attribute set id
require field
and now can add a tab on product page based on attribute set using below format of layout xml
<action method="addCmsBlock" translate="title" module="catalog"><alias>YoutAliasName</alias>
<title>Your Title</title><cmsblock>CmsBlockIdentifier</cmsblock>
<attributeset>YOurAttribute</attributeset></action>
sample Layout code:
<reference name='product.info.tabs'>
<action method="addCmsBlock" translate="title" module="catalog">
<alias>Amit</alias><title>Amit Information</title>
<cmsblock>home-decor</cmsblock><attributeset>13</attributeset>
</action>
</reference>