1. 程式人生 > >magento 獲取產品的銷售量

magento 獲取產品的銷售量

1、如已知產品ID為1

$collection = Mage::getResourceModel('reports/product_sold_collection')
                                ->addOrderedQty()
                                ->setStoreId(1)
                                ->addStoreFilter(1)
                                ->addUrlRewrite()
				->addAttributeToFilter('visibility', array('in' => array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)))
				->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                                ->setOrder('ordered_qty', 'desc')
				->addIdFilter(1);
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()); foreach($collection as $item){ echo '產品',$item->getOrderItemsName(),'的銷售量為:',$item->getOrderedQty(),""; }


2、獲取所有產品的銷售量

$collection = Mage::getResourceModel('reports/product_sold_collection')
                                ->addOrderedQty()
                                ->setStoreId(1)
                                ->addStoreFilter(1)
                                ->addUrlRewrite()
				->addAttributeToFilter('visibility', array('in' => array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)))
				->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                                ->setOrder('ordered_qty', 'desc')
    $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
 foreach($collection as $item){
	echo '產品',$item->getOrderItemsName(),'的銷售量為:',$item->getOrderedQty(),"";
    }