Magento 中按类别分类的新产品

发布于 2024-09-09 10:39:50 字数 1109 浏览 1 评论 0原文

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

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

假设我想显示类别 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 版本。 1.3.2.4,即使使用不同的模板也显示相同的结果。

任何建议都会受到欢迎祝你

有美好的一天,J.PS

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

This should be supposed to be silly thing but it’s driving me nuts!

All I want is to show the new products for a specified category, but all I get is new products from any category.

Supposing I want to show up the category 74, I’ve tried almost any combination of that code

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

With a category_id directly assigned

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

With category implicit depending or which category I’m accessing

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

This code has been tested, hardcoded in home page code, creating a static block with it and including it through category panel (static block and products)…but worthless. I’ve tested almost any code I’ve found in this and other forums but no result.

Regardless of any code (with category_id="74" assigned and not), I’m all the time getting the same result. The category filter doesn’t work and shows me new products from any category, not the new products of the current category, neither the products of a handcoded category (for instance category_id="74")

On the other hand if I use list products instead of new products, works OK in home page and as static block, so categories seem to be well created

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

That shows me products that belong to category 74

I’m using magento Magento ver. 1.3.2.4, and shows me the same result even using different templates.

Any advice will be welcomed

Have a nice day, J.

PS I've tried also with other categories, not only 74. With parent categories, child categories...but no result at all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美胚控场 2024-09-16 10:39:50

最后非常简单...

使用下面的定义块:

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

要检索category_id,您应该像这样修改新类的_beforeToHtml函数:

File in \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

享受...

Finally very simple...

Use the definition block below :

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

To retrieve the category_id, you should modifiy the _beforeToHtml function of the new class like this :

File in \app\code\core\Mage\Catalog\Block\Product\New.php
Prefer surcharge this file in local path

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

You can see that an if statement has been added :

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

You retrieve the category_id with the function $this->getData('category_id')

Enjoy...

怪我鬧 2024-09-16 10:39:50

catalog/product_new 块不接受category_id 作为参数。如果您想对单个类别执行此操作,则需要创建自己的源自 catalog/product_new 的自定义块,并重写方法 _beforeToHtml 以利用 Category_id 。

希望有帮助!

谢谢,

The catalog/product_new block doesn't accept a category_id as a parameter. If you want to do this for a single category, you'll need to create your own custom block that descends from catalog/product_new and override the method _beforeToHtml to utilize a category_id.

Hope that helps!

Thanks,
Joe

栖竹 2024-09-16 10:39:50

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


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

变成这样(getStoreId 可选)...


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

...显然你必须创建像“setCategory”、“getCategory”这样的 getter 和 setter

I think that you want to alter the New.php like this:


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

becomes this (getStoreId optional)...


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

...obviously you will have to create getter and setter like 'setCategory', 'getCategory'

2024-09-16 10:39:50

这对我有用。

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

This works for me.

    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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文