magento根据sku显示产品销售量,这个功能最好放在产品页中显示
如果放在产品目录及列表页中显示,估计需要动用较多的数据库资源,没加速缓冲时谨慎使用。
以下是示范代码
1
2
3
4
5
6
7
8
9
|
$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;
|
或者是以下方式,大同小异
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
function getQuantityOrderedBySku($sku) { try
{ $_product = Mage::getResourceModel( 'reports/product_collection' ) ->addOrderedQty() ->addAttributeToFilter( 'sku' , $sku) ->setOrder( 'ordered_qty' ,
'desc' ) ->getFirstItem(); if
(!$_product) { throw
new Exception( 'No product matches the given SKU' ); } return
( int )$_product->getOrderedQty(); } catch
(Exception $e) { return
0; } } |