Magento: Display Category Image by ID in CMS page - magento

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.

Related

Show Joomla article tags in override of newsflash module

We would like to create an override for the Joomla newsflash module which should be able to display the individual Joomla article tags on the frontend:
"tag 1, tag 2, tag 3 etc.."
RokSproket from rockettheme.com is able to achieve exactly that output. But we would like to have the same result with the newsflash module. We currently have the following code saved in our new _item.php override. There is no error message on the frontend but also no tags are visible. Any help is much appreciated.
<?php if ($params->get('show_tags', 1) && !empty($item->tags)) : ?>
<?php $item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>
<?php echo $item->tagLayout->render($item->tags->itemTags); ?>
<?php endif; ?>
Here's how we've solved that question:
<?php
$itemtags = (new JHelperTags)->getItemTags('com_content.article', $item->id);
$taglayout = new JLayoutFile('joomla.content.tags');
$tags='';
if( !empty($itemtags) )
$tags = '<div class="itemtags">'.str_replace(',','',$taglayout->render($itemtags)).'</div>';
?>
<?php echo $tags; ?>

Magento - Unable to display custom options

I'm using a module for displaying grouped configurable products and all options are showing except custom options. They are displaying on the configurable product page but that's it. I'm trying to use the code in app\design\frontend\blank\blank\template\catalog\product\view\options.phtml in my custom configurable.phtml but $_options is showing up null. Here is the code used to retrieve $_options
<?php $_options = Mage::helper('core')->decorateArray($this->getOptions()) ?>
<?php if (count($_options)):?>
and after the javascript
<?php foreach($_options as $_option): ?>
<?php echo $this->getOptionHtml($_option) ?>
<?php endforeach; ?>
</dl>
<?php else: echo dlkghflghf;?>
<?php endif; ?>
The dlkghflghf is displaying so i know $_options is not showing up. Any suggestions?
Your custom configurable.phtml is a template of what block? You can use a block that extends Mage_Catalog_Block_Product_View_Options or you can set your own method for options like the method from Mage_Catalog_Block_Product_View_Options, you also need to have a method getProduct():
public function getOptions()
{
return $this->getProduct()->getOptions();
}

Add custom category attribute to the frontend.

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;
}

How to make second root category navigation in magento?

Other than the normal navigation I get when I add subcategories to the main root category, I want to be able to make a new root category, assign subcategories to it and have it display as a separate menu.
Is this possible?
May this can help you :Link 1Link 2
To retrieve another root category
<?php
echo '<pre>';
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id); // where $id will be the known category id
if(!empty($categories))
{
echo 'category name->'.$categories->getName(); //get category name
echo '<br>';
echo 'category image url->'.$categories->getImageUrl(); //get category image url
echo '<br>';
echo 'category url path->'.$categories->getUrlPath(); //get category url path
echo '<br>';
}
?>
now $id=9; is my new root category id.
To retrieve sub categories of these new root category ($id=9;) below is the following reference code.Customize it according to your requirements.
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
In an ideal world you would have found the answer by now, but in case you didn't I modified Nikhil's answer to work for me to basically do what you describe, minus any convenience at all...
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id);
if(!empty($categories))
{
$cats = explode(",", $categories->getChildren());
foreach($cats AS $c)
{
$cat = $catagory_model->load(trim($c));
echo ''.$cat->getName().'';
}
}
I'm just pasting what I used. The reality is you will have to build the html to make this do whatever you want it to do. If you have subcategories within your loop, you will have to run another fetch in the foreach part.
I am not familiar with magento enough to know what methods you can run on the $cat object. I did a print_r($cat) to examine the object and made a lucky guess that getUrlKey would be available.
Magento... pfft! you'd think ebay would have higher standards than this.

Setting model as template data (attribute) for child block

In my product view template i'm loading child template, and transfering product instance to be available in this child template:
<?php
echo $this->getLayout()
->createBlock('core/template')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->setAttribute('product', $_product)
->toHtml();
?>
Then in my catalog/product/view/addedToCartDialog.phtml i'm trying to use this product instance:
<?php $product = $this->getData('product'); ?>
<?php echo"<pre>";print_r($product->getId());echo"</pre>"; ?>
However it seems to be not loaded: Fatal error: Call to a member function getId() on a non-object in /home/ryba/workspace/polcode/Greenlights/app/design/frontend/default/greenlights/template/catalog/product/view/addedToCartDialog.phtml on line 2
But when i check variable $product with print_r:
<?php echo"<pre>";print_r($product);echo"</pre>"; ?>
It is displayed that this variable is correct Mage_Catalog_Model_Product Object, also checked if attributes is correct (like sku, name etc) - everything is correct.
What is wrong with this ?
I'm going to give you several answers. The first is the direct answer to your question. The rest are alternatives, but better ways to do what you're trying. The last answer is, in my opinion, the best.
Direct Answer:
Instead of using setAttribute, just use the magic setter/getter methods:
<?php
// In catalog/product/view.phtml
echo $this->getLayout()
->createBlock('core/template')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->setProduct($_product)
->toHtml();
?>
<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>
Better:
And, if you know you are in a template loaded by the catalog/product controller, you can get the product this way.
<?php
// In catalog/product/view.phtml
echo $this->getLayout()
->createBlock('core/template')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->toHtml();
?>
<?php
// In addedToCartDialog.phtml
$_product = Mage::registry('product');
echo $_product->getId();
?>
Even Better
The best way would be to use a different block type which has the methods already loaded (again, if you know you are in a template loaded by the catalog/product controller)
<?php
// In catalog/product/view.phtml
echo $this->getLayout()
->createBlock('catalog/product_view')
->setTemplate('catalog/product/view/addedToCartDialog.phtml')
->toHtml();
?>
<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>
And Finally, the Best
One last item of business. The better way to add extra blocks to your templates is to add the block in your local.xml file.
<!-- Local.xml -->
<catalog_product_view translate="label">
<reference name="content">
<block type="catalog/product_view" name="addedToCartDialog" as="addedToCartDialog" template="catalog/product/view/addedToCartDialog.phtml" />
</reference>
</catalog_product_view>
Now, set up your phtml file
<?php
// In addedToCartDialog.phtml
$_product = $this->getProduct();
echo $_product->getId();
?>
Then call the block from your phtml file
// In catalog/product/view.phtml
<?php echo $this->getChildHtml('addedToCartDialog'); ?>

Resources