Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

[Optimize] Module for products
 
Mageho
Sr. Member
 
Avatar
Total Posts:  291
Joined:  2008-03-24
France, Paris
 

Hi everybody,

I want to introduce my suggestion for some features to present our products.
We have talking about some bugs in block promotion or new products so first, this is the new code for these blocks.

It detects automatically if we are on a category or not, and display products related from on current naviguation.
Promotion

class Mage_Catalog_Block_Product_List_Promotion extends Mage_Catalog_Block_Product_Abstract
{
    
private $_page_size 8;

    public function 
__construct()
    
{
        parent
::__construct();

        
$storeId    Mage::app()->getStore()->getId();
        
$product    Mage::getModel('catalog/product');

        
$todayDate  $product->getResource()->formatDate(time());
        
        
$products   $product->setStoreId($storeId)->getCollection()
          ->
addAttributeToFilter('special_price', array('gt' => 0), 'left')
          ->
addAttributeToFilter('special_from_date', array('date' => true'to' => $todayDate), 'left')
            
          ->
addAttributeToFilter(array(
            array(
'attribute' => 'special_to_date''date' => true'from' => $todayDate),
            array(
'attribute' => 'special_to_date''is' => new Zend_Db_Expr('null'))
          ), 
'''left')
            
          ->
addAttributeToSelect(array('name''price''small_image''thumbnail'), 'inner')
          ->
addAttributeToSelect(array('special_price''special_from_date''special_to_date'), 'left');
              
        if (
$category Mage::registry('current_category')) {
            $products 
$products->addCategoryFilter($categoryfalsetrue);
        
}
        
        Mage
::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

        
$products->setPageSize($this->_page_size)->setCurPage(1);

        
$this->setProductCollection($products);
    
}
}

New products

<?php
/*
 * New products block
 *
 * @category   Mage
 * @package    Mage_Catalog
 */
 
class Mage_Catalog_Block_Product_List_New extends Mage_Catalog_Block_Product_Abstract
{
    
private $_page_size 5;

    public function 
__construct()
    
{
        parent
::__construct();

        
$storeId Mage::app()->getStore()->getId();
        
$product Mage::getModel('catalog/product');
        
        
/* @var $product Mage_Catalog_Model_Product */
        
$todayDate $product->getResource()->formatDate(time());

        
$products $product->setStoreId($storeId)->getCollection()
            ->
addAttributeToFilter(array(array('attribute'=>'news_to_date''date'=>true'from'=>$todayDate), array('attribute'=>'news_to_date''is' => new Zend_Db_Expr('null'))),'','left')
            ->
addAttributeToSelect(array('name''price''small_image'), 'inner')
            ->
addAttributeToSelect(array('special_price''special_from_date''special_to_date'), 'left')
        ;
        
        if (
$category Mage::registry('current_category')) {
            $products 
$products->addCategoryFilter($categoryfalsetrue);
        
}

        Mage
::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

        
$products->setPageSize($this->_page_size)->setCurPage(1);
        
        
$this->setProductCollection($products);
    
}
}

Change method addCategoryFiler, and add a new parameter in
app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php (I want this outside of magento core, but I don’t know how to do this for a model)

public function addCategoryFilter(Mage_Catalog_Model_Category $category$renderAlias=false$getChildren=false)
    
{
        
if (($getChildren === true) || $category->getIsAnchor()) {
            $categoryCondition 
$this->getConnection()->quoteInto('{{table}}.category_id IN (?)'explode(','$category->getAllChildren()));
            
$this->getSelect()->group('e.entity_id');
        
}
        
else {
            $categoryCondition 
$this->getConnection()->quoteInto('{{table}}.category_id=?'$category->getId());
        
}
        
if ($renderAlias{
            $alias 
'category_'.$category->getId();
        
}
        
else {
            $alias 
'position';
        
}

        $this
->joinField($alias'catalog/category_product''position''product_id=entity_id'$categoryCondition);
        return 
$this;
    
}

Impossible to passing the number of products via layout xml or cms ?
I try $this->getNumProducts() with num_products="5" but nothing !

On the other hand, I think it’s a good idea to put a cache system on these module.
So, I am working on it, but if somebody know how to do that, I appreciate.

One more time, i apologize for my lame english…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Essaouera
Member
 
Total Posts:  45
Joined:  2008-01-22
 

Hello Ryu007,

Thanks for those contributions!

I have one question:
I try to display some promotions but only on my Home page.
I used your addAttributeToFilter(...) but it shows all product and not only the promotion:

class Mage_Catalog_Block_Product_Specials extends Mage_Catalog_Block_Product_Abstract
{
    
public function __construct()
    
{
        parent
::__construct();

        
$storeId Mage::app()->getStore()->getId();

        
$product Mage::getModel('catalog/product');
        
/* @var $product Mage_Catalog_Model_Product */
        
$todayDate  $product->getResource()->formatDate(time());
        
$products   $product->setStoreId($storeId)->getCollection()
          ->
addAttributeToFilter('special_price', array('gt' => 0), 'left')
          ->
addAttributeToFilter('special_from_date', array('date' => true'to' => $todayDate), 'left')
          ->
addAttributeToFilter(array(
            array(
'attribute' => 'special_to_date''date' => true'from' => $todayDate),
            array(
'attribute' => 'special_to_date''is' => new Zend_Db_Expr('null'))
          ), 
'''left')
          ->
addAttributeToSelect(array('name''price''small_image''short_description'), 'inner')
          ->
addAttributeToSelect(array('special_price''special_from_date''special_to_date'), 'left');

        
$products->getSelect()->order('rand()');

        
/* @var $products Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */

        
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

        
$products->setPageSize(3)->setCurPage(1);

        
$this->setProductCollection($products);
    
}
}

Do you know why my products are not filter by “special_price”?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Phil Chris
Jr. Member
 
Total Posts:  3
Joined:  2009-03-17
Thiruvananthapuram, Kerala, India
 

im also having the same prob ......... 

any help?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top