magento-静态块中的类别名称和图像? [英] magento - category name and image in static block?
问题描述
如何通过magento后端从magento的静态块中引用类别名称和图像?我正在运行1.7.
How can i reference category name and image from a static block in magento through the magento backend? I'm running 1.7.
推荐答案
我不知道可以从静态块中轻松引用这些值的方法.
I am not aware of a way in which you can easily reference these values from within a static block.
相反,我建议您创建和使用一个小部件(我认为是Magento最未被充分利用的功能之一),它将提供一种更清洁,更可扩展的方式来实现此目的-尽管它确实需要做更多的工作:)
Instead, I would suggest you create and use a widget (one of the most underused features of Magento in my opinion) which will provide a much cleaner and more extendible way of achieving this - though it does require more work upfront :)
请参见下面的代码,以获取完整的(简化的)Magento小部件示例,该示例完全符合您在静态块中的要求:
Please see code below for a full (simplified) example of a Magento Widget which does exactly what you have asked from the static block:
app/etc/modules/YourCompany_Categorywidget.xml
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
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
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 for 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
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
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
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 be set for 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
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>
这篇关于magento-静态块中的类别名称和图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!