Magento分类的新产品

时间:2010-07-15 10:36:01

标签: magento categories

这应该是愚蠢的事情,但它让我疯了!

我想要的只是展示指定类别的新产品,但我得到的只是来自任何类别的新产品。

假设我想要显示类别74,我几乎尝试了该代码的任何组合

{{block type="catalog/product_new" name="home.catalog.product.new" alias="product_homepage" category_id="74" template="catalog/product/new.phtml"}}

直接分配category_id

{{block type="catalog/product_new" category_id="74" template="catalog/product/new.phtml"}} 

使用类别隐式依赖或我正在访问的类别

{{block type="catalog/product_new" template="catalog/product/new.phtml"}}

此代码已经过测试,在主页代码中进行了硬编码,使用它创建了一个静态块,并通过类别面板(静态块和产品)包含它......但是没有价值。我已经测试了几乎我在这个和其他论坛中发现的任何代码但没有结果。

无论任何代码(分配了category_id =“74”而不是),我总是得到相同的结果。类别过滤器不起作用,向我显示任何类别的新产品,而不是当前类别的新产品,也不是手工编码类别的产品(例如category_id =“74”)

另一方面,如果我使用列表产品而不是新产品,可以在主页和静态块中正常工作,因此类别似乎很好创建

{{block type="catalog/product_list" category_id="74" template="catalog/product/list.phtml"}}

这显示了属于74类的产品

我正在使用magento Magento ver。 1.3.2.4,即使使用不同的模板也会显示相同的结果。

欢迎任何建议

祝你有个美好的一天,J。

PS我也尝试过其他类别,不仅仅是74.对于父类别,子类别......但根本没有结果

4 个答案:

答案 0 :(得分:3)

最后很简单......

使用下面的定义块:

{{block type="catalog/product_new" name="home.catalog.product.new" alias="allCatalogNewProducts" category_id="5" template="catalog/product/new.phtml"}}

要检索category_id,您应该修改新类的_beforeToHtml函数,如下所示:

\ app \ code \ core \ Mage \ Catalog \ Block \ Product \ New.php中的文件 更喜欢在本地路径中附加此文件

   protected function _beforeToHtml()
    {
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
            ->addAttributeToFilter('news_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToSort('news_from_date', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1)
        ;

      if($categoryId=$this->getData('category_id')){
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $collection->addCategoryFilter($category);
      }


        $this->setProductCollection($collection);

        return parent::_beforeToHtml();

您可以看到添加了if语句:

  if($categoryId=$this->getData('category_id')){
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $collection->addCategoryFilter($category);
  }

使用函数$ this-> getData('category_id')

检索category_id

...享受

答案 1 :(得分:1)

catalog/product_new块不接受category_id作为参数。如果您要为单个类别执行此操作,则需要创建自己的自定义块,该块来自catalog/product_new并覆盖方法_beforeToHtml以使用category_id。

希望有所帮助!

谢谢, 乔

答案 2 :(得分:0)

这适合我。

    protected function _beforeToHtml()
    {
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
            ->addAttributeToFilter('news_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToSort('news_from_date', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1)
        ;


        if($categoryId=$this->getCategoryId()){
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $collection->addCategoryFilter($category);
}

        $this->setProductCollection($collection);

        return parent::_beforeToHtml();
    }

答案 3 :(得分:0)

我认为您想要像这样改变New.php:

$collection = Mage::getResourceModel('catalog/product_collection');

成为这个(getStoreId可选)......

$featCat = Mage::getModel('catalog/category')->load($this->getCategory()); $collection = Mage::getResourceModel('catalog/product_collection')->setStoreId($this->getStoreId())->addCategoryFilter($featCat);

...显然你必须创建像'setCategory','getCategory'

这样的getter和setter
相关问题