php - magento - category name and image in static block? -


how can reference category name , image static block in magento through magento backend? i'm running 1.7.

i not aware of way in can reference these values within static block.

instead, suggest create , use widget (one of underused features of magento in opinion) provide cleaner , more extendible way of achieving - though require more work upfront :)

please see code below full (simplified) example of magento widget have asked static block:

app/etc/modules/yourcompany_categorywidget.xml

<config>     <modules>         <mycompany_categorywidget>             <active>true</active>             <codepool>community</codepool>         </mycompany_categorywidget>     </modules> </config> 

app/code/community/mycompany/categorywidget/etc/config.xml

<?xml version="1.0"?>  <config>     <modules>         <mycompany_categorywidget>             <version>1.0.0</version>         </mycompany_categorywidget>     </modules>     <global>         <blocks>             <categorywidget>                 <class>mycompany_categorywidget_block</class>             </categorywidget>         </blocks>         <helpers>             <categorywidget>                 <class>mycompany_categorywidget_helper</class>             </categorywidget>         </helpers>     </global> </config> 

app/code/community/mycompany/categorywidget/etc/widget.xml

<?xml version="1.0"?>  <widgets>     <category_widget type="categorywidget/catalog_category_widget_info" translate="name description" module="categorywidget">         <name>category info block</name>         <description>category info block showing name, image etc</description>         <parameters>             <block_title translate="label">                 <required>1</required>                 <visible>1</visible>                 <label>block title</label>                 <type>text</type>             </block_title>             <template>                 <required>1</required>                 <visible>1</visible>                 <label>template</label>                 <type>select</type>                 <value>categorywidget/info.phtml</value>                 <values>                     <default translate="label">                         <value>categorywidget/info.phtml</value>                         <label>category widget info block - default template</label>                     </default>                     <!-- add different temmplates here different block positions -->                 </values>             </template>             <category translate="label">                 <visible>1</visible>                 <required>1</required>                 <label>category</label>                 <type>label</type>                 <helper_block>                     <type>adminhtml/catalog_category_widget_chooser</type>                     <data>                         <button translate="open">                             <open>select category...</open>                         </button>                     </data>                 </helper_block>                 <sort_order>10</sort_order>             </category>         </parameters>     </category_widget> </widgets> 

app/code/community/mycompany/categorywidget/helper/data.php

<?php  class mycompany_categorywidget_helper_data extends mage_core_helper_abstract {} 

app/code/community/mycompany/categorywidget/block/catalog/category/widget/info.php

<?php  class mycompany_categorywidget_block_catalog_category_widget_info     extends mycompany_categorywidget_block_catalog_category_info         implements mage_widget_block_interface {     protected function _preparecategory()     {         $this->_validatecategory();          $category = $this->_getdata('category');         if (false !== strpos($category, '/')) {             $category = explode('/', $category);             $this->setdata('category', (int)end($category));         }         return parent::_preparecategory();     } } 

app/code/community/mycompany/categorywidget/block/catalog/category/info.php

<?php  class mycompany_categorywidget_block_catalog_category_info extends mage_core_block_template {     protected $_category;      protected function _beforetohtml()     {         $this->_category = $this->_preparecategory();         return parent::_beforetohtml();     }      protected function _preparecategory()     {         $this->_validatecategory();         return mage::getmodel('catalog/category')->load($this->_getdata('category'));     }      protected function _validatecategory()     {         if (! $this->hasdata('category')) {             throw new exception('category must set info block');         }     }      public function getcategoryname()     {         return $this->_category->getname();     }      public function getcategoryimage()     {         return $this->_category->getimageurl();     } } 

app/design/frontend/base/default/template/categorywidget/info.phtml

<?php     $_categoryname = $this->getcategoryname();     $_categoryimage = $this->getcategoryimage();  ?>   <div class="categoryinfo_block block">     <p><strong><?php echo $_categoryname ?></strong></p>     <img src="<?php echo $_categoryimage ?>" alt="<?php echo $_categoryname  ?>" />  </div> 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -