フッター用の「今月の製品」ブロックを作成しています。カテゴリの商品を読み込み、最初の商品を表示する必要があります。
これは私のテンプレートファイルです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