Tag-Archive for » bestseller «

TO RESOLVE BESTSELLER RESOURCE COLLECTION ISSUE WITH FLAT CATALOG

TO RESOLVE BESTSELLER RESOURCE COLLECTION ISSUE WITH FLAT CATALOG PRODUCT

You need to override MAGENTOSITEDIRECTORY\app\code\core\Mage\Reports\Model\Resource\Product\Collection.php

public function addOrderedQty($from = ”, $to = ”)

Rewrite resource Model Mage_Reports_Model_Resource_Product_Collection
Add below function to custom rewrite model

getConnection();
        $compositeTypeIds     = Mage::getSingleton('catalog/product_type')->getCompositeTypes();
        $orderTableAliasName  = $adapter->quoteIdentifier('order');

        $orderJoinCondition   = array(
            $orderTableAliasName . '.entity_id = order_items.order_id',
            $adapter->quoteInto("{$orderTableAliasName}.state <> ?", Mage_Sales_Model_Order::STATE_CANCELED),

        );
        
 
        $productJoinCondition = array(
            $adapter->quoteInto('(e.type_id NOT IN (?))', $compositeTypeIds),
            'e.entity_id = order_items.product_id',
            $adapter->quoteInto('e.entity_type_id = ?', $this->getProductEntityTypeId())
        );

        if ($from != '' && $to != '') {
            $fieldName            = $orderTableAliasName . '.created_at';
            $orderJoinCondition[] = $this->_prepareBetweenSql($fieldName, $from, $to);
        }

        $this->getSelect()->reset()
            ->from( 
                array('order_items' => $this->getTable('sales/order_item')),
                array(
                    'ordered_qty' => 'SUM(order_items.qty_ordered)',
                    'order_items_name' => 'order_items.name'
                ))
            ->joinInner(
                array('order' => $this->getTable('sales/order')),
                implode(' AND ', $orderJoinCondition),
                array());
        if ($this->isEnabledFlat()) { /*If condition Added for flat catalog*/
        	 $this->getSelect()->joinLeft(
	                array('e' => Mage::getResourceSingleton('catalog/product_flat')->getFlatTableName()),
	               "e.entity_id = order_items.product_id"  );
        } else {
	          $this->getSelect()->joinLeft(
	                array('e' => $this->getProductEntityTableName()),
	                implode(' AND ', $productJoinCondition),
	                array(
	                    'entity_id' => 'order_items.product_id',
	                    'entity_type_id' => 'e.entity_type_id',
	                    'attribute_set_id' => 'e.attribute_set_id',
	                    'type_id' => 'e.type_id',
	                    'sku' => 'e.sku',
	                    'has_options' => 'e.has_options',
	                    'required_options' => 'e.required_options',
	                    'created_at' => 'e.created_at',
	                    'updated_at' => 'e.updated_at'
	                ));
	        }
            $this->getSelect()->where('parent_item_id IS NULL')
            ->group('order_items.product_id')
            ->having('SUM(order_items.qty_ordered) > ?', 0);
        return $this;
    }


?>


I hope above solution works like charm.

Thanks,

Bijal Bhavsar :)