【发布时间】:2012-09-05 15:48:23
【问题描述】:
我正在为页脚构建一个“本月产品”块。它应该加载一个类别的产品并显示第一个。
这是我的模板文件custom/featured-product.phtml:
<?php $_productCollection = $this->getLoadedProductCollection() ?>
<div class="featured-product">
<h2><?php echo $this->__('Product of the Month') ?></h2>
<?php foreach ($_productCollection as $_product): ?>
<div class="item">
<a class="product-image" href="<?php echo $_product->getProductUrl() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
<a class="product-name" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>
<?php echo $this->getPriceHtml($_product, true) ?>
</div>
<?php
// Note: Exit after first product.
break;
?>
<?php endforeach ?>
</div>
这只是 Magento 产品列表模板的简化版:catalog/product/list.phtml
工作中
在 CMS 页面中嵌入块时,它可以正常工作。示例:
{{block type="catalog/product_list" category_id="13" template="custom/featured-product.phtml" }}
不工作
当通过local.xml 嵌入块时,它会失败。返回正确的标记,但未加载指定的类别。而是加载了随机(我不知道它们是如何选择的)一组产品。
我的代码在local.xml:
<default>
<reference name="footer">
<block type="catalog/product_list" name="custom.featuredProduct" as="product_of_the_month" category_id="13" template="custom/featured-product.phtml" />
</reference>
</default>
为了完整起见,我在page/html/footer.phtml 中显式渲染该块,如下所示:
<?php echo $this->getChildHtml('product_of_the_month') ?>
有什么想法吗?
我的最佳猜测是我的local.xml 不正确。我需要加载父块吗?
[更新]
我的原始代码使产品页面崩溃。解决方法是不将代码如此依赖于 Magento 核心文件:catalog/product/list.phtml。特别避开这一行:
<?php $_productCollection = $this->getLoadedProductCollection() ?>
[解决方案]
这里包含一个带有用于 CMS 页面和 LayoutXML 示例的工作版本: https://stackoverflow.com/a/12288000/1497746
【问题讨论】:
-
local.xml- 这个文件在哪里? (完整路径) -
@FlorinelChis — 这不是主题回退层次结构的问题。这是自定义包的正常位置:/app/design/frontend/custom/custom/layout/local.xml
标签: magento