我正在尝试自定义我的产品着陆页,以包含同一类别中所有其他产品的所有可 ScrollView 。我几乎已经有了它,但它显示了所有类别的产品,而不仅仅是您正在查看的当前产品所在的类别。 下面是我添加到我的产品 view.phtml 中的代码,它为我提供了可滚动容器中的产品。任何人都可以帮我限制它只显示当前类别中的产品吗?可以在 Product view page 查看其中一个产品页面的示例。
版本是magento 1.6.1
我的产品在这里查看定制
<div class="coll_container">
<!-- "previous page" action -->
<a class="prev browse left"></a>
<div id="collection" class="scrollable">
<?php
$cat_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); // set current category id
$category = Mage::getModel('catalog/category')->load($cat_id);
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category);
$products = $category->getProductCollection()->addCategoryFilter($category)->addAttributeToSelect('*');
?>
<ul>
<?php foreach ( $products as $_product ): ?>
<li class="item"><?php if($_product->isComposite() || !$_product->isSaleable()): ?>
<?php endif; ?>
<a class="item-link" href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->keepAspectRatio(true)->keepFrame(false)->resize(150,225) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" width="150" height="225" /></a>
<div class="tooltip">
<span class="indicator"><!-- --></span>
<div class="tooltip-content">
<a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>
<!-- Price -->
<p class="price"> <?php echo $this->getPriceHtml($_product, true) ?> </p>
</div><!-- End Tooltip content -->
</div><!-- End Tooltip -->
</li>
<?php endforeach; ?>
</ul>
</div><!-- End Collection -->
<!-- "next page" action -->
<a class="next browse right"></a>
</div><!-- End coll_container -->
最佳答案
除了两次获取集合的冗余 - $productCollection
和 $products
,后者用于循环 - 您的代码将正常工作并且集合将仅包含当前类别的产品,提供您的着陆页被重写为
catalog/category/view/id/<id> OR
catalog/product/view/id/<id>/category/<id>
但不是,如果它们被重写为
catalog/product/view/id/<id>
这是因为 Mage_Catalog_Model_Layer::getCurrentCategory()
取决于注册表项是否为 current_category
已设置。如果current_category
未设置时,该方法返回当前商店的根类别 ID,这解释了为什么您会看到所有 类别的产品:
public function getCurrentCategory()
{
$category = $this->getData('current_category');
if (is_null($category)) {
if ($category = Mage::registry('current_category')) {
$this->setData('current_category', $category);
}
else {
$category = Mage::getModel('catalog/category')->load($this->getCurrentStore()->getRootCategoryId());
$this->setData('current_category', $category);
}
}
return $category;
}
在 Magento OOB 中,注册表项 current_category
将设置在不同的位置,但在您的情况下,只有这两个是感兴趣的:
Mage_Catalog_CategoryController::_initCatagory()*
Mage_Catalog_Helper_Product::initProduct()
前者通常只被调用
Mage_Catalog_CategoryController::viewAction()
后者在多个地方被调用,其中包括
Mage_Catalog_ProductController::viewAction()
但仅在这种情况下,如果 catalog
参数如 catalog/<id>
通过了。
重写
解决您的问题的一种可能性是为您的着陆页添加重写
catalog/product/view/id/1/category/3
风格target_path
如果您只有少数产品,您可以使用后台管理中的“URL 重写管理”手动执行此操作。
如果你有很多产品,你也可以这样做programmatically .
直接使用产品的类别 ID
解决此问题的另一种可能性是,直接从正在查看的产品中获取类别 ID 并使用它来过滤集合:
$aCategoryId = $this->getProduct()->getCategoryIds();
if (!$aCategoryId) {
echo "This product is not assigned to any categories.";
}
$iCategoryId = $aCategoryId[0];
$oCategory = Mage::getModel('catalog/category')->load($iCategoryId);
$oProductCollection = $oCategory
->getProductCollection()
->addCategoryFilter($oCategory)
->addAttributeToSelect('*')
->addFieldToFilter('entity_id', array('neq' => $this->getProduct()->getId()));
foreach ($oProductCollection as $oProduct) {
var_dump($oProduct->getName());
}
请注意,我添加了一个过滤器以从集合中排除查看的产品本身。
* 那不是错字,它真的写成了 _initCatagory
, 不是 _initCategory
至少 CE 1.6.2(还没有检查其他人)
关于magento-1.6 - Magento 在产品 View 页面上显示类别内的所有产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11993953/