1、获取某类目下,某属性的属性值,多少产品拥有它。
$categoryId = 27; //可以把类目条件去掉,可获得所有
$storeId = 1;
$attributeId = 273;
$category = Mage::getModel('catalog/category')->load($categoryId);
$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($storeId)
->addCategoryFilter($category);
$select = clone $collection->getSelect();
// reset columns, order and limitation conditions
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->reset(Zend_Db_Select::LIMIT_COUNT);
$select->reset(Zend_Db_Select::LIMIT_OFFSET);
$conn = Mage::getSingleton("core/resource")->getConnection('core_read');
$select
->join(
array('product_eav' => "catalog_product_index_eav"),
join(' AND ', array(
"product_eav.entity_id = e.entity_id",
$conn->quoteInto("product_eav.attribute_id = ?", $attributeId),
$conn->quoteInto("product_eav.store_id = ?", $storeId),
)),
array('value', 'count' => new Zend_Db_Expr("COUNT(product_eav.entity_id)")))
->group("product_eav.value");
$result = $conn->fetchPairs($select);
var_dump($result);
返回结果:array('optionId' => $num);
2、查询某属性值,有多少产品拥有它(配置产品)
//Note: B.entity_id 就是配置产品的ID
select A.*,count(*) as num from eav_attribute_option_value as A
left join catalog_product_index_eav as B on B.value=A.option_id and B.store_id=1
where A.store_id=0 and B.store_id=1 and B.entity_id>0
group by A.option_id
limit 20
结果: