New Products by Category at Magento

It must be stupid, but it drives me crazy!

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

Suppose I want to show category 74, Ive tried almost any combination of this code

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

When assigning category_id

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

With a category implicit in dependency or which category Im access to

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

This code has been tested, hardcoded into the code of the home page, creating a static block with it and turning it on through the categories panel (static block and products) ... but it's useless. Ive checked almost any Ive code found on this and other forums, but no result.

Regardless of any code (with the assignment of category_id = "74" and not), Im gets the same result all the time. The category filter does not work and shows me new products from any category, not new products of the current category, nor products of the category with manual input (for example, category_id = "74")

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

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

It shows me category 74 products

Im using magento Magento ver. 1.3.2.4, and shows the same result even when using different templates.

Any advice would be welcome.

Have a nice day, J.

PS I also tried other categories, not only 74. With parent categories, child categories ... but there was no result at all

+4
source share
4 answers

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 get category_id, you must modify the _beforeToHtml function of the new class as follows:

File in \ app \ code \ core \ Mage \ Catalog \ Block \ Product \ New.php Prefer this file in the local path for an additional fee

  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 the if statement added:

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

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

Enjoy ...

+3
source

The catalog/product_new block does not accept category_id as a parameter. If you want to do this for one category, you need to create your own custom block that descends from catalog/product_new and override the _beforeToHtml method to use category_id.

Hope this helps!

Thanks Joe

+1
source

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

I think you want to modify New.php as follows:

$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 need to create a getter and setter, for example, "setCategory", "getCategory"

0
source

Source: https://habr.com/ru/post/1315784/


All Articles