首页上的 Magento 类别描述
Magento category description on frontpage
我有一家 Magento 商店,想在首页上显示带有描述和图片的热门类别。有谁知道我该如何解决这个问题?
我用这个来显示热门类别:
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
但我无法让它同时显示说明和图像。
希望有人能帮我解决这个问题。
此致,
罗伯特
您可以使用模块实现此目的:
注意:在这种方法中,您可以显示要显示的块(主页、页脚、侧边栏、其他页面)。
步骤:
这里,
[命名空间]=Codilar&
[模块名称]=类别
创建 xml : app/etc/modules/Codilar_Category.xml
<?xml version="1.0"?>
<config>
<modules>
<Codilar_Category>
<active>true</active>
<codePool>local</codePool>
</Codilar_Category>
</modules>
</config>
创建 config.xml: app/code/local/Codilar/Category/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Codilar_Category>
<version>1.0</version>
</Codilar_Category>
</modules>
<global>
<blocks>
<codilar_category>
<class>Codilar_Category_Block</class>
</codilar_category>
</blocks>
<models>
<codilar_category>
<class>Codilar_Category_Model</class>
</codilar_category>
</models>
</global>
</config>
创建模型:app/code/local/Codilar/Category/Model/Category。php
<?php
class Codilar_Category_Model_Category extends Mage_Core_Model_Abstract {
public function getActiveCategory() {
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter();
return $categories;
}
}
创建区块:app/code/local/Codilar/Category/Block/Category.php
<?php
class Codilar_Category_Block_Category extends Mage_Core_Block_Template {
public function getActiveCategory() {
$arr_categories = array();
$categories = Mage::getModel("codilar_category/category")->getActiveCategory();
foreach ($categories as $category) {
$arr_categories[] = array(
'id' => $category->getId(),
'name' => $category->getName(),
'url' => $category->getProductUrl(),
'image' => $category->getThumbnail(),
'desc' => $category->getDescription(),
);
}
return $arr_categories;
}
}
创建模板文件:
app/design/frontend/default/default/template/codilar/category/category.phtml
<?php
$categories = $this->getActiveCategory();
?>
<div id="category_list">
<h1>All Category</h1>
<?php if (is_array($categories) && count($categories)) { ?>
<?php foreach($categories as $category) { ?>
<div>
<a href="<?php echo $category['url'] ?>"><?php echo $category['name'] ?></a>
</div>
<div>
<img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'. $category['image']; ?>" alt="<?php echo $category['image'] ?>" height="100" width="100">
</div>
<div>
<p><?php echo $category['desc'] ?></p>
</div>
<?php } ?>
<?php } ?>
</div>
要使用布局更新文件插入您的类别自定义块,您可以使用以下代码。
<block type="codilar_category/category" name="codilar_category_category" template="codilar/category/category.phtml"></block>
另一方面,如果您想使用 CMS 页面显示您的类别自定义块,请使用以下代码。
{{block type="codilar_category/category" name="codilar_category_category" template="codilar/category/category.phtml"}}
我使用以下代码显示每个顶级类别的 2 种产品:
$count = 0;
$_productCollection = Mage::getModel('catalog/category')->load($_categoryId)
->getProductCollection()
->addAttributeToSelect('*')
->setOrder('date', 'ASC');
?>
<div class="home-featured-product">
<?php foreach ($_productCollection as $_product): ?>
<li class="item">
<div class="regular">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(270, 200); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
</div>
<a class="product-name" href="<?php echo $_product->getProductUrl() ?>">
<?php echo $this->htmlEscape($_product->getName()) ?>
</a>
<?php $productObject = Mage::getModel('catalog/product')->load($_product->getId());?>
<?php echo $this->getPriceHtml($productObject, true) ?>
</li>
<?php
if($count == 1) break; // Stop after 2 items
$count++;
?>
<?php endforeach ?>
</div>
但我还需要显示描述和价格,但不知何故 getPriceHtml 似乎不起作用:(
我有一家 Magento 商店,想在首页上显示带有描述和图片的热门类别。有谁知道我该如何解决这个问题?
我用这个来显示热门类别:
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
但我无法让它同时显示说明和图像。 希望有人能帮我解决这个问题。
此致, 罗伯特
您可以使用模块实现此目的:
注意:在这种方法中,您可以显示要显示的块(主页、页脚、侧边栏、其他页面)。
步骤: 这里, [命名空间]=Codilar& [模块名称]=类别
创建 xml : app/etc/modules/Codilar_Category.xml
<?xml version="1.0"?>
<config>
<modules>
<Codilar_Category>
<active>true</active>
<codePool>local</codePool>
</Codilar_Category>
</modules>
</config>
创建 config.xml: app/code/local/Codilar/Category/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Codilar_Category>
<version>1.0</version>
</Codilar_Category>
</modules>
<global>
<blocks>
<codilar_category>
<class>Codilar_Category_Block</class>
</codilar_category>
</blocks>
<models>
<codilar_category>
<class>Codilar_Category_Model</class>
</codilar_category>
</models>
</global>
</config>
创建模型:app/code/local/Codilar/Category/Model/Category。php
<?php
class Codilar_Category_Model_Category extends Mage_Core_Model_Abstract {
public function getActiveCategory() {
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter();
return $categories;
}
}
创建区块:app/code/local/Codilar/Category/Block/Category.php
<?php
class Codilar_Category_Block_Category extends Mage_Core_Block_Template {
public function getActiveCategory() {
$arr_categories = array();
$categories = Mage::getModel("codilar_category/category")->getActiveCategory();
foreach ($categories as $category) {
$arr_categories[] = array(
'id' => $category->getId(),
'name' => $category->getName(),
'url' => $category->getProductUrl(),
'image' => $category->getThumbnail(),
'desc' => $category->getDescription(),
);
}
return $arr_categories;
}
}
创建模板文件: app/design/frontend/default/default/template/codilar/category/category.phtml
<?php
$categories = $this->getActiveCategory();
?>
<div id="category_list">
<h1>All Category</h1>
<?php if (is_array($categories) && count($categories)) { ?>
<?php foreach($categories as $category) { ?>
<div>
<a href="<?php echo $category['url'] ?>"><?php echo $category['name'] ?></a>
</div>
<div>
<img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'. $category['image']; ?>" alt="<?php echo $category['image'] ?>" height="100" width="100">
</div>
<div>
<p><?php echo $category['desc'] ?></p>
</div>
<?php } ?>
<?php } ?>
</div>
要使用布局更新文件插入您的类别自定义块,您可以使用以下代码。
<block type="codilar_category/category" name="codilar_category_category" template="codilar/category/category.phtml"></block>
另一方面,如果您想使用 CMS 页面显示您的类别自定义块,请使用以下代码。
{{block type="codilar_category/category" name="codilar_category_category" template="codilar/category/category.phtml"}}
我使用以下代码显示每个顶级类别的 2 种产品:
$count = 0;
$_productCollection = Mage::getModel('catalog/category')->load($_categoryId)
->getProductCollection()
->addAttributeToSelect('*')
->setOrder('date', 'ASC');
?>
<div class="home-featured-product">
<?php foreach ($_productCollection as $_product): ?>
<li class="item">
<div class="regular">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(270, 200); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
</div>
<a class="product-name" href="<?php echo $_product->getProductUrl() ?>">
<?php echo $this->htmlEscape($_product->getName()) ?>
</a>
<?php $productObject = Mage::getModel('catalog/product')->load($_product->getId());?>
<?php echo $this->getPriceHtml($productObject, true) ?>
</li>
<?php
if($count == 1) break; // Stop after 2 items
$count++;
?>
<?php endforeach ?>
</div>
但我还需要显示描述和价格,但不知何故 getPriceHtml 似乎不起作用:(