Magento:如何显示产品销售次数?

时间:2011-10-18 13:44:27

标签: magento

我想在我的主页上回应一下产品的总销售额。怎么办呢?

此外,我想知道如何查询首页上的已售商品总数(数量)。

在我的商店中,只有1个产品(虚拟)。

修改

我找到了这段代码,它在产品的视图页面中运行良好。

$sku = nl2br($_product->getSku());
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToFilter('sku', $sku)
    ->setOrder('ordered_qty', 'desc')
    ->getFirstItem();
$product = $_productCollection;

echo 'Already Bought '.(int)$product->ordered_qty; 

但是在首页上,我如何直接指向我想要的产品的ID?

4 个答案:

答案 0 :(得分:5)

这应该有效:

$id = 123; // enter your product ID here
$product = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToFilter('id', $id)
    ->setOrder('ordered_qty', 'desc')
    ->getFirstItem();

echo 'Already Bought '.(int)$product->ordered_qty;

答案 1 :(得分:3)

//This will show  total sales of all the products 
<?php    
     foreach(Mage::getModel('catalog/product')->getCollection() as $product)
     {
            $productId= $product->getId();
            $productModel = Mage::getModel('catalog/product');
            $name = $productModel->load($productId)->getName();
            echo "<br/>";
            $product = Mage::getResourceModel('reports/product_collection')
                ->addOrderedQty()
                ->addAttributeToFilter('entity_id', array('eq' => $productId))
                ->setOrder('ordered_qty', 'desc')
                ->getFirstItem();
            echo (int)$product->ordered_qty.'&nbsp'.$name.' Have Been Sold';
        }
?> 

答案 2 :(得分:0)

代码应该是下一个:

$id = $_product->getId();
    $product = Mage::getResourceModel('reports/product_collection')
        ->addOrderedQty()
        ->addAttributeToFilter('entity_id', array('eq' => $id))
        ->setOrder('ordered_qty', 'desc')
        ->getFirstItem();
    echo 'Downloaded '.(int)$product->ordered_qty.'times';

这段代码很适合我(显示多少次, curent产品已售出)

答案 3 :(得分:0)

按照列表页面上的最高销售产品展示产品

$i=0;  
foreach($_collection as $product)  
     {  
            $productId= $product->getId();  
            $productModel = Mage::getModel('catalog/product');  
            $name = $productModel->load($productId)->getName();  
            $product1 = Mage::getResourceModel('reports/product_collection')  
                ->addOrderedQty()  
                ->addAttributeToFilter('entity_id', array('eq' =>     $productId))  
                ->setOrder('ordered_qty', 'desc')  
                ->getFirstItem();  
            $solditems[$i]= array($product1->ordered_qty,$product);  
            $i++;  
        }    
            rsort($solditems);  
print_r($solditems);