【问题标题】:Magento Display all products within the category on a product view pageMagento 在产品视图页面上显示该类别中的所有产品
【发布时间】:2012-08-16 19:15:04
【问题描述】:

我正在尝试自定义我的产品登录页面,以包含同一类别中所有其他产品的所有可滚动视图。我几乎就在那里,但它显示了所有类别的产品,而不仅仅是您正在查看的当前产品所在的类别。 下面是我添加到我的产品 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 -->

【问题讨论】:

    标签: magento-1.6


    【解决方案1】:

    除了两次获取集合的冗余性 - $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/&lt;id&gt; 这样的 catalog 参数。

    重写

    解决您的问题的一种方法是为您的目标网页添加重写

    catalog/product/view/id/1/category/3
    

    风格target_paths.

    如果您只有少数产品,您可以使用后台管理中的“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());
    }
    

    请注意,我添加了一个过滤器以从集合中排除查看的产品本身。


    * 这不是错字,至少在 CE 1.6.2 中确实写成 _initCatagory,而不是 _initCategory(还没有检查其他人)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多