此代码(1)的目的是以网格形式显示Magento存储中的子类别。类别图像的大小在代码本身中定义,而分隔和宽度在样式中定义.css。
如果我正常使用代码,我会得到所有启用的子类别(IsActive 是真的)。请参阅此"正在进行的工作"链接:http://www.aldetal.biz/index.php/vabaro/fitness/nutricion.html
我更愿意显示已启用的子类别,并在其他地方标记为"值得显示"。这样,我将登录页面中以网格形式显示的子类别数量与菜单中显示的子类别数量分开。为了避免接触数据库,我将使用元关键字字段添加字符串 showgrid 作为"关键字"标志。
法典
<?php
/**
* Original code by Jake Rutter
* @website: http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html#idc-ctools
* Fixed by func0der
*
*/
?>
<div id="categories">
<div class="col_full">
<div class="listing" >
<?php
$_maincategorylisting = $this->getCurrentCategory();
$_categories = $this->getCurrentChildCategories();
if($_categories->count()):
foreach ($_categories as $_category):
/** Choose between the two possible IFs. The first one works (as before, incompletely. The second Should work completely but instead breaks the code */
/** if($_category->getIsActive()): */
if($_category->getIsActive() && preg_match('/^showgrid/ism', $_category->getData('meta_keywords'))):
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
/** This is just for debugging. The first shows the sub-category name under the image (as it should in final version). The second one shows the text in Meta_Keywords. */
/**
/** $catName = $this->getCurrentCategory()->getName(); */
$catName = $this->getCurrentCategory()->getData('meta_keywords');
if($_imageUrl = $this->getCurrentCategory()->getImageUrl()):
?>
<div class="category-box">
<div class="category-image-box">
<a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>" height="150"></a>
</div>
<div class="category-name">
<?php
/** Changed to check writing the category vs variable */
/** <a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a> */
?>
<p>
<a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $catName ?></a>
</p>
</div>
</div>
<?php
else:
?>
<div class="category-box">
<div class="category-image-box">
<a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="/skin/frontend/default/default/images/media/default01.jpg" height="150"); ?></a>
</div>
<div class="category-name">
<p>
<a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $catName ?></a>
</p>
</div>
</div>
<?php
endif; /* END: if($_imageUrl=!$this->getCurrentCategory()->getImageUrl()) */
endif; /* END: $_category->getIsActive()) */
endforeach; /* END: ($_categories as $_category) */
/* This resets the category back to the original pages category
* If this is not done, subsequent calls on the same page will use the last category in the foreach loop.
*
* The next line is the one showing the problem. If I use the expanded IF I get the error here...
*/
$layer->setCurrentCategory($_maincategorylisting);
endif; /* END: if($_categories->count()) */
?>
</div>
<br clear=all>
</div>
</div>
出于某种原因,蒂姆建议的优秀IF
(1)最初由Jake Rutter撰写:http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html 并由我调整
我有以下结构的代码来显示Magento前端中的子类别。子类别在启用时显示("IsActive"为真)。
if($_categories->count()):
foreach ($_categories as $_category):
if($_category->getIsActive()):
a whole bunch of stuff if true
else
other stuff if not true
我想在不弄乱数据库的情况下添加另一个条件,所以我使用了元关键字字段(因为它被谷歌正式忽略了,无论如何拥有一个古怪的关键字不会造成任何伤害)。
如果类别已启用(IsActive),并且该字段中的第一个字符串是"showgrid",则IF为真,并且会发生整堆事情。
但要小心:元关键字字段可能为空,所以我需要测试它并确定其内容。如果未启用类别(IsActive 为 false)或元关键字为空或元关键字不以 showgrid 开头,则会发生其他事情
比元标记字段更好的解决方案是向类别添加新属性。
在 app/code/local/[YourCompany]/Category 中创建一个目录
在app/code/local/[YourCompany]/Category/etc/config中创建你的config.xml文件.xml
<?xml version="1.0"?>
<config>
<modules>
<YourCompany_Category>
<version>0.1.0</version>
</YourCompany_Category>
</modules>
<global>
<resources>
<category_setup>
<setup>
<class>Mage_Eav_Model_Entity_Setup</class>
<module>YourCompany_Category</module>
</setup>
<connection>
<use>default_setup</use>
</connection>
</category_setup>
</resources>
</global>
</config>
接下来在应用程序/etc/模块/YourCompany_Category.xml中创建你的模块配置
<?xml version="1.0"?>
<config>
<modules>
<YourCompany_Category>
<active>true</active>
<codePool>local</codePool>
</YourCompany_Category>
</modules>
</config>
接下来,我们在app/code/local/YourCompany/Category/sql/category_setup/mysql4-install-0.1.0中创建安装程序.php
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_category','category_show_grid', array(
'type' => 'int',
'group' => 'General Information',
'backend' => '',
'frontend' => '',
'label' => 'Show in grid',
'input' => 'select',
'class' => '',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default_value' => 1,
'searchable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false
));
$installer->endSetup();
然后,您应该能够在代码中调用它
if ($_category->getIsActive() && $_category->getCategoryShowGrid()) {
}