1

I have a category called Most Purchasing. Here I need to get most purchased/placed products and assign it to specific category programmatically. How can I achieve this?

Note : Here I need to get product collection based on customer purchased/ordered products.

Any help welcome with thanks.

1
$categoryId = 41; // Your category Id
$category = Mage::getModel('catalog/category')->load($categoryId);
$category->setPostedProducts(array()); // Removes all old products added to your category
$category->save();
$limit = 5; // Number of products to get
$storeId    = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->addAttributeToFilter('status', 1)
            ->addAttributeToFilter('visibility', 4)
            ->setOrder('ordered_qty', 'desc')
            ->setPageSize($limit);

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
foreach($products as $product){
    $product->setCategoryIds($categoryId);
    $product->save();
}
  • Jaimin, thanks a lot for your answer. Here I need to get most purchased products for every 5 mins. For that I have set up Cron Job in my server and as well as my module. But its not working. Why? Here I followed to set up cron – Mesk May 24 '17 at 8:27
  • Are you sure your cron is running successfully? Try adding log message in your function and check if you get the message in var/log file. Once you are confirmed that your cron is working properly, try adding above code in a standalone php file to check if its working for you. – Jaimin Sutariya May 24 '17 at 8:31
  • I have followed below code in my module's config.xml to setup cron job : <config><crontab><jobs><my_cron><schedule><cron_expr>*/5 * * * *</cron_expr></schedule><run> <model>mymodule/observer::doSomething</model> </run></my_cron></jobs></crontab></config> Observer.php Code class Inchoo_MyModule_Model_Observer { public function doSomething() { // Here I have added your code to get most purchased products } } – Mesk May 24 '17 at 8:33
  • Yes the code is correct but first you need to make sure that cron on your server is running. Add Mage::log('Cron Working',null,'cron.log'); in your function and after 5 mins check var/log/cron.log file. If its created and you can see message "Cron Working", your cron is setup correctly. – Jaimin Sutariya May 24 '17 at 8:39
  • The file(cron.log) is not generated under var/log. So whats the problem. In coding? – Mesk May 24 '17 at 9:06
0

I have achieved this by changing some modifications in Jaimin's code. This is the solution :

$bestcategoryId = 19; // Your category Id
$bestcategory = Mage::getModel('catalog/category')->load($bestcategoryId);
$bestcategory->setPostedProducts(array()); // Removes all old products added to your category
$bestcategory->save();

$_storeId = Mage::app()->getStore()->getId();
$_products = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', array(
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH
    ))
    ->setOrder('ordered_qty', 'asc');
$categorybest = array($bestcategoryId);
foreach ($_products as $_product){
    $product = Mage::getModel('catalog/product')->load($_product->getId());
    $categoryb = array_merge($categorybest,$product->getCategoryIds());
    $product->setCategoryIds($categoryb);
    $product->save();

}

Thanks for @Jaimin support.

  • @Jaimin can you please explain this concept of Best Seller? Because customer has purchased some specific product 3 to 4 times over the period of 5 years. But it's listed in that category. How? – Mesk Sep 2 '17 at 8:17
  • @Jaimin this code gives empty results. why? : ` $products = Mage::getResourceModel('reports/product_collection') ->addOrderedQty() ->addAttributeToSelect('*') ->setStoreId($storeId) ->addStoreFilter($storeId) ->addAttributeToFilter('status', 1) ->addAttributeToFilter('visibility', 4) ->setOrder('ordered_qty', 'desc') ->setPageSize($limit); Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products); ` – Mesk Sep 2 '17 at 8:39
  • You are getting the product because you did not limit your collection. I think above code will give you all the products which are purchased. You can limit collection to 5 – Jaimin Sutariya Sep 3 '17 at 1:53
  • Yes , I already limit the product collection to 5 by setting $limit = 5; and I have tried with code you added as answer but its shows nothing. But I am having more no of products as bestseller. Can you please let me know what's the issue? – Mesk Sep 11 '17 at 3:21
  • 1
    I have checked this Jaimin. The variable $_products gives me the products. But I am sure these products are not mostly purchased products. I think we need to change Mage::getResourceModel('reports/product_collection') for best selling products. Is there any alternatives? – Mesk Sep 15 '17 at 7:55

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.