查看有多少产品拥有某属性值

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); 

    143340_sj5J_2715471.png

 

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

    结果:

    144554_wa46_2715471.png

转载于:https://my.oschina.net/ganfanghua/blog/1542860

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值