Php magento如何检查属性值是否分配给任何产品

Php magento如何检查属性值是否分配给任何产品,php,magento,Php,Magento,如何检查属性值是否已分配给任何产品?我有一个值为aa、bb、cc、dd的产品属性a,我想知道是否有任何产品具有aa或bb或cc或dd属性值 i、 e如果产品属性表中的计数(aa)大于0 到目前为止我的代码 /*Get the Attributes for cat id 9 */ $layer = Mage::getModel("catalog/layer"); $layer->setCurrentCategory(Mage::getModel('catalog/category')->

如何检查属性值是否已分配给任何产品?我有一个值为aa、bb、cc、dd的产品属性a,我想知道是否有任何产品具有aa或bb或cc或dd属性值

i、 e如果产品属性表中的计数(aa)大于0

到目前为止我的代码

/*Get the Attributes for cat id 9 */
$layer = Mage::getModel("catalog/layer");
$layer->setCurrentCategory(Mage::getModel('catalog/category')->load(9));
$validAttributes = array();
foreach ($layer->getFilterableAttributes() as $attribute) {
    //allow only select attributes - you can implement your additional filters here
    if ($attribute->getFrontendInput() == 'select'){
        $validAttributes[] = $attribute;
    }
}

$i=0;
$name='';
/*Create the menu html in loop*/
foreach ($validAttributes as $attribute) {

    $options = $attribute->getSource()->getAllOptions();
    $attr=$attribute->getData();
    $name = $attr['frontend_label'];

    $temp.= '<li class=" nav-item level0 nav-1 level-top  level-top first last nav-item--parent classic nav-item--only-subcategories parent">
    <a href="#" class="level-top"> 
        <span>'.$name.'</span>
        <span class="caret">&nbsp;</span> 
    </a>
    <span class="opener"></span><ul class="level0 nav-submenu nav-panel--dropdown nav-panel" style="left: 44px; top: 40px; display: none;">';




    foreach ($options as $option)
    {
            //Here I want to check count($option['value']) in product >0
        $temp.='<li class="nav-item level1 nav-1-1 first last classic" >
                <a href="'.$catUrl.'?'.$attr['attribute_code'].'='.$option['value'].'">
                    <span> '.$option['label'].'</span>
                </a></li>';


    }
    $temp.= '</ul></li>';
    $i++;

}
/*获取cat id 9的属性*/
$layer=Mage::getModel(“目录/层”);
$layer->setCurrentCategory(Mage::getModel('catalog/category')->load(9));
$validAttributes=array();
foreach($layer->getFilterableAttributes()作为$attribute){
//仅允许选择属性-您可以在此处实现附加过滤器
如果($attribute->getFrontendInput()=='select'){
$validAttributes[]=$attribute;
}
}
$i=0;
$name='';
/*在循环中创建菜单html*/
foreach($validates作为$attribute){
$options=$attribute->getSource()->getAllOptions();
$attr=$attribute->getData();
$name=$attr['frontend_label'];
$temp.='
    • 0中的计数($option['value']) $temp.='
    • '; } $temp.='
  • '; $i++; }
    简单高效地使用此代码

    $prod_ids_array=Mage::getModel('catalog/product')->getCollection()
                                  ->addAttributeToFilter('custom_attribute_code',array('notnull' => true))
                                  ->getAllIds();
    print_r($prod_ids_array);
    

    您将获得具有属性值的产品ID。

    您可以通过运行sql查询轻松获得它

    我假设属性id=74,属性类型为整数

    如果它是整数,那么数据将存储在catalog\u product\u entity\u int中

    select entity_id from catalog_product_entity_int where attribute_id = 73 and store_id = 0
    

    现在您有了您要查找的ID列表。

    我知道我来晚了,但这里有一个查询,以防您想改为按属性代码搜索:

    SELECT
      cpe1.sku,
      cpe1.value
    FROM
      catalog_product_entity_int cpei
      LEFT JOIN eav_entity_attribute eav ON eav.entity_attribute_id = cpei.attribute_id
      LEFT JOIN catalog_product_entity cpe1 ON cpe1.row_id = cpei.row_id
      LEFT JOIN eav_attribute ea ON ea.attribute_id = cpei.attribute_id
    WHERE
      ea.attribute_code = "my_attribute_code"
      AND cpe1.attribute_set_id IS NOT NULL;
    
    这仅显示整数属性“catalog\u product\u entity\u int”。根据属性类型,还有其他表:

    • 目录\产品\实体\日期时间
    • 目录\产品\实体\十进制
    • 目录\产品\实体\图库
    • 目录\产品\实体\内部
    • 目录\产品\实体\媒体\图库
    • 目录\产品\实体\文本
    • 目录\产品\实体\层级\价格
    • 目录\产品\实体\ varchar

    可能的重复项我希望它按属性值检查,如其中属性\u code=7和value='canvas',它应返回产品计数或ID我希望它按属性值检查,如其中属性\u code=7和value='canvas',它应返回产品计数或ID