You may Run this code on list page:

This code will update the attribute value based on  all order after run this code remove the code and the next code in success.phtml:

<?php //ensure to set current store as product attributes are store specific Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$productCollection = Mage::getModel('catalog/product')->getCollection();
 
foreach($productCollection as $product) 
{
    $productsku = $product->getSku();
    $_product_Collection = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToFilter('sku', $productsku)
    ->setOrder('ordered_qty', 'desc')
    ->getFirstItem();
    
    $productq = $_product_Collection;
    $_product_oder_qty = (int)$productq->ordered_qty;
    $attributeCode = "bestseller";
    $product = Mage::getModel('catalog/product')
                   ->load($product->getEntityId());
    $product->setData($attributeCode, $_product_oder_qty)
            ->getResource()
            ->saveAttribute($product, $attributeCode);
}
?>

increment the attribute value on each orders:

<?php
$order = Mage::getSingleton('sales/order'); 
$order->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$_items = $order->getAllItems();
//echo count($_items);
foreach($_items as $_item) {
    //echo 'NAME '.$_item->getName().',  QTY '.$_item->getQtyOrdered().'<br/>';
    $qty1 = $_item->getQtyOrdered();
    $product = Mage::getModel('catalog/product')->load($_item->getProductId());
    $qty2 = $product->getResource()->getAttribute('bestseller')->getFrontend()->getValue($product);
    $_product_oder_qty = $qty1+$qty2;
    $attributeCode = "bestseller";
    $product->setData($attributeCode, $_product_oder_qty)
            ->getResource()
            ->saveAttribute($product, $attributeCode);
}
?>

Categories: Magento

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *