In previous project our client wanted to edit in administration which categories will be shown inside footer and left side. By default, Magento doesn't offer any such settings. You also can not add custom attribute to category. You can add it only to products. So let's go step by step how to make this settings available.
First we need to add to categories a custom attribute. We will call it custom_attribute1 with a name "Include in top menu". You can add it directly to database, but a better approach is by module installation. When you create a modul, $installer-startSetup() is called. This is done by
app/code/local/Spletnisistemi/Categoriesatributes/sql/categoriesatributes_setup/mysql4-install-0.1.0.php
This article is not about how to create a module, so I will just write some parts of the module:
-
$installer = $this;
-
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
-
$installer->startSetup();
-
-
'group' => 'General',
-
'input' => 'select',
-
'type' => 'static',
-
'label' => 'Include in footer',
-
'source' => 'eav/entity_attribute_source_boolean',
-
'backend' => '',
-
'visible' => 1,
-
'required' => 0,
-
'user_defined' => 1,
-
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
-
));
-
-
$installer->endSetup();
When the module is installed, admin can set to desired category Include in footer to Yes or No. In this way, we can get a list of categories that need to be displayed.
We can created a block called Printcategories.php.
app/code/local/Spletnisistemi/Categoriesatributes/Block/Printcategories.php
-
<?php
-
-
class Spletnisistemi_Categoriesatributes_Block_Printcategories extends Mage_Core_Block_Template {
-
-
public function drawFooterItem()
-
{
-
$categories = Mage::getModel('catalog/category')
-
->getCollection()
-
->addAttributeToSelect('*')
-
->addAttributeToFilter('custom_attribute1', 1)
-
->setPageSize(3);
-
return $categories;
-
}
-
-
}
and template printfooterlinks.phtp
app/design/frontend/default/default/categoriesatributes/printfooterlinks.phtp
-
<ul class="footMenu">
-
<?php
-
$_categories = $this->drawFooterItem();
-
foreach($_categories as $_category):
-
$_child_categories = $_category->getChildrenCategories();?>
-
<ul>
-
<?php $n = 0; foreach($_child_categories as $_child_category){ ?>
-
<li><a href="<?php echo $_child_category->getUrl(); ?>"><?php echo $_child_category->getName(); ?></a></li>
-
<?php }; ?>
-
</ul>
-
</li>
-
<?php endforeach; ?>
-
</ul>
In final template, we can print categories in the following way:
-
<?php echo $this->getLayout()->createBlock('categoriesatributes/printcategories')->setTemplate('categoriesatributes/printfooterlinks.phtml')->toHtml(); ?>
Podobni članki:
- Magento – Adding custom attributes with module install
- Magento – add attribute to attribute programmaticaly with sql
- Add SQL queries with new Magento extension version
- Magento – how to call template or block inside category or other view
- Magento – Grand total and subtotal price doesn’t show in order review
- Magento – how to count all products with php – Magento product collection model?
- Magento – Display cost at category listing page
- Magento – mass update magento attribute back to Use Default Value
- Magento – modify product view without changing the view.phtml
- Magento – namestitev dodatnega atributa poleg modula