Question

I'm trying to get in the home page 12 bestseller products with this code:

$_productCollection = Mage::getResourceModel('reports/product_collection')
                                                    ->addAttributeToSelect('*')
                                                    ->addOrderedQty()
                                                    ->setStoreId($storeId)
                                                    ->addStoreFilter($storeId) 
                                                    ->setOrder('ordered_qty', 'desc');

The problem is that with this code I have the bestsellers of all stores.

The parameter ->setStoreId() and ->addStoreFilter don't work.

Any idea?

EDIT

With the code of Rakesh Donga get the next list: enter image description here

These products have the status "disabled" in backend.

In the backend go to Reports -> Products -> Products ordered and get this products:

enter image description here

Était-ce utile?

La solution 2

SOLUTION

Need override the file:

"Mage/Catalog/Model/Resource/Product/Collection.php"

Add the next function at the end of the file:

 public function addWebsiteFilterPurchased()
{

    $storeId = Mage::app()->getRequest()->getParam('store');
    if($storeId == null){
        $storeId = Mage::app()->getStore()->getStoreId();
    }

    $this->getSelect()->where('order_items.store_id IN (?)', (array)$storeId);

    return $this;
}

And use it:

$_productCollection = Mage::getResourceModel('reports/product_collection')
                                                    ->addAttributeToSelect('*')
                                                    ->addOrderedQty()
                                                    ->addWebsiteFilterPurchased()
                                                    ->setOrder('ordered_qty', 'desc');

Autres conseils

Did you try using following code:

Mage_Reports_Model_Resource_Product_Collection


/**
 * Add store restrictions to product collection
 *
 * @param  array $storeIds
 * @param  array $websiteIds
 * @return Mage_Reports_Model_Resource_Product_Collection
 */
public function addStoreRestrictions($storeIds, $websiteIds)
{
    if (!is_array($storeIds)) {
        $storeIds = array($storeIds);
    }
    if (!is_array($websiteIds)) {
        $websiteIds = array($websiteIds);
    }

    $filters = $this->_productLimitationFilters;
    if (isset($filters['store_id'])) {
        if (!in_array($filters['store_id'], $storeIds)) {
            $this->addStoreFilter($filters['store_id']);
        } else {
            $this->addStoreFilter($this->getStoreId());
        }
    } else {
        $this->addWebsiteFilter($websiteIds);
    }

    return $this;
}

So your case:


$_productCollection = Mage::getResourceModel('reports/product_collection')
                                                    ->addAttributeToSelect('*')
                                                    ->addOrderedQty()
                                                    ->addStoreRestrictions([$storeId], [$websiteIds])
                                                    ->setOrder('ordered_qty', 'desc');
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top