首页 文章

Magento的新产品

提问于
浏览
4

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

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

假设我想要显示类别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 回答

  • 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

    catalog/product_new 块不需要创建自己的自定义块,该块来自 catalog/product_new 并覆盖方法 _beforeToHtml 以使用category_id .

    希望有所帮助!

    谢谢,乔

  • 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

  • 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();
        }
    

相关问题