在产品视图中仅显示当前类别的产品
我已经在这个问题上苦苦挣扎了很长时间,看起来很容易解决,但我自己做不到。
该产品与 2 个不同的类别相关联,我想展示仅此当前类别的产品列表(在本例中为 ID 188),而不是来自该产品列出的所有猫。这就像按“current_cat_Id”或其他内容过滤此列表。
当前的代码是这样的:
<div class="box base-mini mini-related-items">
<?php
$test = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
echo 'Current Main Category of this product this list should show:'.$test;
?>
<?php
if ($_product) {
// get collection of categories this product is associated with
$categories =$_product->getCategoryCollection()
//->setPage(1, 1) //selects only one category
->addFieldToFilter('level','4') //selects only 3rd level categories
//->addFieldToFilter('parent_id','188' ) //select only child categories of no 3
// ->setOrder("level") //combined by setPage, returns the lowest level category
->load();
// if the product is associated with any category
if ($categories->count())
foreach ($categories as $_category)
{
$cur_category = Mage::getModel('catalog/category')->load($_category->getId());
?>
<div class="head"><h4>Todos os produtos da coleção <strong><?=$cur_category->getName()?> (Id: <?=$cur_category->getId()?>)</strong></h4></div>
<div class="content">
<ol>
<? $products = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($_category)
->addAttributeToSelect('small_image');
foreach ( $products as $productModel )
{
$_product = Mage::getModel('catalog/product')->load($productModel->getId());
$width=50; $height=50;
$_imageUrl = $this->helper('catalog/image')->init($productModel, 'small_image')->resize($width, $height);
?>
<li<?php if($_product->isComposite() || !$_product->isSaleable()): ?> class="super-products"<?php endif; ?> class="product-box">
<div class="product-images">
<a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->resize(50) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" width="50" height="50" /></a>
</div>
<div class="product-details">
<a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>
<!-- Price -->
<?php echo $this->getPriceHtml($_product, true) ?>
</div>
</li>
<? }
echo "</ol><div class=\"clear\"></div></div>";
}
}
?>
</div>
有人可以帮我解决这个问题吗? 预先感谢您的帮助!
干杯, 杰威
I’ve been struggling with this question for a long time and it seems very easy to solve, I just can’t do it by myself.
Take a look at this page: http://adegean.dominiotemporario.com/porcelanas-brancas/artigos-de-mesa/linha-americana/saladeira-pequena-americana.html
This product is associated with 2 different categories, and I would like to show only a list of products of this current category (in this case, ID 188), not from all the cats the product is listed in. It’s just something like filtering this list by “current_cat_Id” or something.
The current code is this:
<div class="box base-mini mini-related-items">
<?php
$test = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
echo 'Current Main Category of this product this list should show:'.$test;
?>
<?php
if ($_product) {
// get collection of categories this product is associated with
$categories =$_product->getCategoryCollection()
//->setPage(1, 1) //selects only one category
->addFieldToFilter('level','4') //selects only 3rd level categories
//->addFieldToFilter('parent_id','188' ) //select only child categories of no 3
// ->setOrder("level") //combined by setPage, returns the lowest level category
->load();
// if the product is associated with any category
if ($categories->count())
foreach ($categories as $_category)
{
$cur_category = Mage::getModel('catalog/category')->load($_category->getId());
?>
<div class="head"><h4>Todos os produtos da coleção <strong><?=$cur_category->getName()?> (Id: <?=$cur_category->getId()?>)</strong></h4></div>
<div class="content">
<ol>
<? $products = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($_category)
->addAttributeToSelect('small_image');
foreach ( $products as $productModel )
{
$_product = Mage::getModel('catalog/product')->load($productModel->getId());
$width=50; $height=50;
$_imageUrl = $this->helper('catalog/image')->init($productModel, 'small_image')->resize($width, $height);
?>
<li<?php if($_product->isComposite() || !$_product->isSaleable()): ?> class="super-products"<?php endif; ?> class="product-box">
<div class="product-images">
<a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->resize(50) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" width="50" height="50" /></a>
</div>
<div class="product-details">
<a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>
<!-- Price -->
<?php echo $this->getPriceHtml($_product, true) ?>
</div>
</li>
<? }
echo "</ol><div class=\"clear\"></div></div>";
}
}
?>
</div>
Could someone please help me solving that??
Thank you in advance for your help!
Cheers,
jw
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 http://www.magentocommerce.com/boards/viewthread/51638/我终于得出了答案。以下代码在 view.html 页面中效果很好:
Tks to http://www.magentocommerce.com/boards/viewthread/51638/ I finally came to an answer. The following code works great in he view.html page: