1

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

  • store id how to get it? – Rakesh Donga Feb 11 at 16:57
  • with this: $storeId = Mage::app()->getStore()->getId(); – str_lml Feb 11 at 16:58
  • here another store products coming? – Rakesh Donga Feb 11 at 16:59
  • No, return the correct id but the collection of products are about all the websites – str_lml Feb 11 at 17:00
  • check my answer – Rakesh Donga Feb 11 at 17:06
1

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');
  • I put this: ->addStoreRestrictions([$storeId], [Mage::getModel('core/store')->load($storeId)->getWebsiteId()]) and return an incorrect list – str_lml Feb 11 at 17:05
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.