2012-11-02 30 views
1

您好,我想知道如何編寫php語句來執行一些sql查詢。magento如何以最低價格列出最受歡迎的產品

例如:(列出所有產品以最小的代價)

$collection1 = Mage::getModel('catalog/product')->getCollection(); 
$collection1 
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) 
->addAttributeToFilter('attribute_set_id', 4) 
->setOrder('created_at', 'desc') 
->addMinimalPrice() 
->addFinalPrice() 
->addTaxPercents(); 

SQL查詢是這樣說的:

SELECT `e`.*, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, 
IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, 
`price_index`.`tier_price` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_index_price` AS 
`price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND 
price_index.customer_group_id = 0 WHERE (`e`.`attribute_set_id` = '4') ORDER BY `e`.`created_at` desc 

但我知道下面的PHP語句可以列出觀看次數最多的產品:

$collection2 = Mage::getResourceModel('reports/product_collection'); 
$collection2->addViewsCount(); 

然後,sql查詢就是這樣的:

SELECT COUNT(report_table_views.event_id) AS `views`, `e`.* FROM `report_event` AS `report_table_views` 
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = report_table_views.object_id AND e.entity_type_id = 4 
WHERE (report_table_views.event_type_id = 1) GROUP BY `e`.`entity_id` HAVING 
(COUNT(report_table_views.event_id) > 0) ORDER BY `views` DESC 

我在想怎麼寫PHP語句結合上述兩個SQL查詢,我想說明從下面的SQL查詢的結果。

SELECT COUNT(report_table_views.event_id) AS `views`, `e`.*, `price_index`.`price`, `price_index`.`tax_class_id`, 
`price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, 
price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, 
`price_index`.`max_price`, `price_index`.`tier_price` FROM `report_event` AS `report_table_views` INNER JOIN 
`catalog_product_entity` AS `e` ON e.entity_id = report_table_views.object_id AND e.entity_type_id = 4 INNER JOIN 
`catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = 
'1' AND price_index.customer_group_id = 0 WHERE (report_table_views.event_type_id = 1 AND `e`.`attribute_set_id` = 
'4') GROUP BY `e`.`entity_id` HAVING (COUNT(report_table_views.event_id) > 0) ORDER BY `views` DESC 

我以爲我可以寫下面的PHP語句,但它不工作,也許addViewsCount()是覆蓋整個SQL語句。

$collection3 = Mage::getResourceModel('reports/product_collection'); 

$collection3 
->addAttributeToSelect('*') 
->addAttributeToFilter('attribute_set_id', 4) 
->addOrderedQty() 
->addMinimalPrice() 
->addFinalPrice() 
->addTaxPercents() 
->addViewsCount(); 

現在,誰又能知道怎麼寫PHP語句,以最小的價格上市觀看次數最多的產品,請原諒我,我有一個屬性集ID必須爲4對所有產品實體麻煩的要求。我知道這個問題是非常具有挑戰性的,因爲我不知道從谷歌搜索這些信息。請大家看看問題。感謝每一個努力。

+0

我認爲這些PHP語句正在工作。 \t $ storeId = Mage :: app() - > getStore() - > getId(); \t $ collection3 = Mage :: getResourceModel('reports/product_collection') - > setStoreId($ storeId); \t $ collection3-> addViewsCount(); \t $ collection3-> addAttributeToFilter('attribute_set_id',4); \t $ collection3-> addAttributeToSelect('*'); \t $ collection3-> addStoreFilter($ storeId); \t $ collection3-> addMinimalPrice(); \t $ collection3-> addFinalPrice(); \t $ collection3-> addTaxPercents(); – Jun

回答

3

試着這麼做

$productCount = 5; 

//Get Store ID 

$storeId = Mage::app()->getStore()->getId();  

// Get most viewed product collection 

$products = Mage::getResourceModel('reports/product_collection') 
    ->addAttributeToSelect('*')  
    ->setStoreId($storeId) 
    ->addStoreFilter($storeId) 
    ->addAttributeToSelect(array('name', 'price', 'minimal_price', 'small_image')) 
    ->addViewsCount() 
    ->setPageSize($productCount); 

Mage::getSingleton('catalog/product_status') 
     ->addVisibleFilterToCollection($products); 
Mage::getSingleton('catalog/product_visibility') 
     ->addVisibleInCatalogFilterToCollection($products); 

print_r($products->getData()); 

更多詳情:

1
<?php $productCount = 5; 
$storeId = Mage::app()->getStore()->getId(); 
$products = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->setStoreId($storeId) ->addStoreFilter($storeId) ->addViewsCount() ->setPageSize($productCount); Mage::getSingleton('catalog/product_status') ->addVisibleFilterToCollection($products); 
Mage::getSingleton('catalog/product_visibility') ->addVisibleInCatalogFilterToCollection($products); 
print"<pre>"; 
print_r($products); ?> 
相關問題