Add custom category attribute to the frontend. - magento

Pages of different category should display different footers and is the idea behind.
I know there are lot of similar questions and I tried them all, but they didn't work.
I have added custom category attribute to category page, and I want to see it in the footer.
I tried to use:
<?php if($_customAttribute = $this->getCurrentCategory()->getCustomAttribute()): ?>
<?php echo $_helper->categoryAttribute($_category, $_customAttribute, 'footer_text') ?>
But I have not got any results for this.

You can try it like this.
$category = Mage::registry('current_category');
<?php if ($category->getFooterText()) : ?>
<?php echo Mage::helper('catalog/output')->categoryAttribute($category, $category->getFooterText(), 'footer_text');?>
<?php endif;?>
But keep in mind...if you add this in footer.phtml it won't work with the block cache on. The footer is cached, so once you load a page the code above will have no effect on the next pages.
[EDIT]
If you want to have a different footer on some pages you need to modify the cache key for the footer block. There is an explanation on how to do that in here but it's for a simple page not a category. The concept is the same.
What you need to do is to change only the getCacheKeyInfo method to depend on the category. Something like this:
public function getCacheKeyInfo()
{
$info = parent::getCacheKeyInfo();
$category = Mage::registry('current_category');
if ($category) {
$info[] = $category->getId();
}
return $info;
}

Related

Magento - how to filter categories in phtml

I have been working on the maga menu. I got the collection in phtml like:
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
Now I have to add the filter to show specific categories. Like I have an array(1,2,3,4) of categories that i want to show. so how can I apply filter to this Helper.
Any one have any suggestion please answer.
Thanks.
The first answer is correct but it's not efficient as it consumes unnecessary database round trips. #Karan's code issues a query to the database for every id. Just imagine if the number of category ids to be filtered were 50 or above.
My example would be this:
<?php
$catIds = array(1,2,3,4);
$catCollection = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('id', $catIds)->addAttributeToFilter('is_active',1);
foreach($catCollection as $category){
echo $category->getName()." ";
}
This will reduce the database roundtrip to just one.
use this code
<?php $catids[]=array(1,2,3,4);
foreach($catids as $id):
$_category = Mage::getModel('catalog/category')->load($id);
if($_category->getIsActive()):
echo $_category->getName();
endif;
endforeach;
?>
and don't forget to link my answer if it was helpful

check current category of layered navigation in magento

get current category in layer navigation,im using this code but it is not working please help
<?php $_helper = $this->helper('catalog/output') ?>
<?php $_category = $this->getCurrentCategory(); >
please use the below code
if(Mage::registry('current_category'))
{
$cat=Mage::registry('current_category')->getId();
/* used for category level*/
}
else{
$cat=2; // 2 is root category id
/* used for serch level*/
}
hope it will be working
If you want to get Current Category id on navigation, you can do like this.
<?php $layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();?>
Hopes this will solve your problem.

Populate custom attributes from database Magento

I needed to add a custom attribute to categories on Magento, I have been able to do so using:
http://www.atwix.com/magento/add-category-attribute/
But now, I will like to turn the custom attribute into a select box and load the options from a external database/module. Is there any way to specificy the options available for the custom attribute in order to make them dinamic?
Thanks.
Have you found the solution? I'm doing it in phtml file like this
<?php $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*'); ?>
<?php foreach ($categories as $category): ?>
<?php $customAtt= $category->getCustom_att(); ?>
<?php print_r ($customAtt); ?>
<?php endforeach; ?>
That way will populate all your custom category attribute values. Change the getCustom_att() with your custom attribute.
To make it a select option list, read this https://magento.stackexchange.com/questions/8674/how-to-get-all-custom-category-attribute-values/8692#8692

Show fatal error when click to product detail page from wishlist page

I am trying to display category name in product detail page. For that I am using
$cat_name=Mage::registry('current_category')->getName();
It shows category name.
But when I went to wishlist page & click to product image then it give error:-
Fatal error: Call to a member function getName() on a non-object in /opt/lampp/htdocs/dominie/app/design/frontend/default/dominie/template/catalog/product/view.phtml.
Please help me how can I solve this issue.
Just tested the code below and it work in v1.7 when added to template/catalog/product/view.phtml.
However Mage::registry('current_category'); is only available if when coming to a product page from a category page (not tested, but may also dependent on if you have seo url that contain the category names within the url)
<?php
$_helper = $this->helper('catalog/output');
$_category_detail=Mage::registry('current_category');
echo $_category_detail->getName(); //gives current category name
echo $_category_detail->getId(); //gives current category id
?>
See http://vinayp.com.np/how-to-get-category-name-and-id-on-product-page-on-magento/
To display all the categories that a product belong to do
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<?php echo $_category->getName() ?>
<?php endforeach; ?>
See http://www.magentocommerce.com/boards/viewthread/27720/
Try to define $cat_name in your wishlist module so that collection from wishlist module will also contain category name from which it was added to wishlist. This will surely reduce your overhead.

Magento: Display Category Image by ID in CMS page

Is there a way to display a category image by it's category id in a CMS page?
You can display a category link by id with:
{{widget type="catalog/category_widget_link" anchor_text="Displayed Text" title="Title attribute text" template="catalog/category/widget/link/link_block.phtml" id_path="category/22"}}
Or display category products by it's id.
{{block type="catalog/product_list" category_id="14" template="catalog/product/list.phtml"}}
But I can't figure out hwo to display a specific categories image in a CMS page just by calling it's id.
Any idea?
In stock magento you cant.
There are essentially 3 ways of achieving this, in order of preference (good to bad imo):
Create and use a widget
Embed directly in cms page and use a custom block type for initialising the category
Embed directly in a cms page, use core/template block type and initialise category inside the template directly
1. Use a widget
I have previously answered a similar question and provided a complete example on how to build such a widget specific to a category:
magento - category name and image in static block?
2. Custom module
You nned to create a module with a block to support this. You can then just include the block in cms pages using the following syntax:
{{block type="yourmodule/category_image" category_id="14" template="yourmodule/category/image.phtml"}}
Your block class is going to look like this:
<?php
class Yourcompany_Yourmodule_Block_Category_Image extends Mage_Core_Block_Template
{
public function getCategory()
{
if (! $this->hasData('category')) {
$category = Mage::getModel('catalog/category')->load($this->getData('category_id'));
$this->setData('category', $category);
}
return $this->getData('category');
}
}
and your template class is going to look like this:
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>
<?php if($_imgUrl): ?>
<?php echo $_imgHtml ?>
<?php endif; ?>
3. Use core/template block type
Include on the cms page like so..
{{block type="core/template" category_id="14" template="category/image.phtml"}}
Create your template - catalog/category/image.phtml
<?php
$_helper = $this->helper('catalog/output');
$category = Mage::getModel('catalog/category')->load($this->getData('category_id'));
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>
<?php if($_imgUrl): ?>
<?php echo $_imgHtml ?>
<?php endif; ?>
I don't think you'll be able to just call the image because it is not a block. In /design/base/default/template/category/view.phtml, it's just calling the image url as an attribute of the block and then building the output HTML right in the template file. There isn't any specific element in the block that renders it to call with magento's shortcode.
Best best would be to build a new widget. There's plenty of guides, and they're pretty awesome once you get to know them better.

Resources