繁体   English   中英

Magento:无法使用自定义属性过滤器来获取产品集合?

[英]Magento: Cant get product collection use custom attribute filter?

我的magento目录中有一些产品被标记为采样器。 我需要撤回该特定集合。 该属性是称为糖果的属性集的一部分。

当我去加载集合时,我添加了一个过滤器以缩小属性集,然后为我创建的采样器属性添加另一个过滤器。 无论我对过滤器做什么,我总是会取回所有糖果,而不仅仅是采样器属性设置为“是”或1的糖果。

这是我的代码,如何获得所需的结果?

 $products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')  //select all fields available to product
    ->addFieldToFilter('attribute_set_id', 9) //filter the collection by the candies attribute set
    ->addAttributeToFilter('sampler','yes'); //filter the collection to only products that have the attribute sampler set to "yes" - this part doesnt work.

提前致谢。

请参阅: http : //gielberkers.com/magento-attributes-missing-using-flat-catalog/

在考虑了此处的评论之后,我将尝试此设置。

您将遍历整个集合。 加载每个产品的ID,然后使用-> getAttributeText('')。

    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
    ->addAttributeToFilter('type_id', 'configurable')
    ;
    foreach ($collection as $_product) {
        $id =  $_product->getId();
        if($tncValue = $_product->load($id)->getAttributeText('terms_and_conditions')){
            echo $id . ' => ' . $tncValue . "\n";
        }
    }
<?php
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('attribute_set_id', 9)
        ->addAttributeToFilter('sampler', array('eq' => '1'));
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM