您好我想知道如何编写用于执行某些sql查询的php语句。
例如:(以最低价格列出所有产品)
$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 :(得分:4)
尝试类似
的内容$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 :(得分: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); ?>