1

How can I get the products with a attribute (attribute name is "use_state") value of "new"? I added an attribute called "use_state" and an attribute set with that attribute. Then I created some products that have this attribute set and assigned value "new" to these products' "use_state" attribute. For the world, I cannot anyhow get these products in my block, in order to be able to display them. I tried these approaches and each one was just giving me some useless information that I don't even understand, instead of the concrete products information, that I actually expect from them. I tried this:

$collection = Mage::getModel('catalog/product') -> getCollection(); 
$collection -> addAttributeToSelect(array('name'));
$collection -> addFieldToFilter("use_state", "new");

print "DEV: <pre>"; print_r($collection); print "</pre>";

$_products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name'))
->addAttributeToFilter('use_state', 'new')
->load();

echo "DEV: <pre>"; print_r($_products); echo "</pre>";

Not giving me any products, just some info that I don't even understand. Can you help me see what's wrong here?

I have just found out that this line might be the culprit:

->addAttributeToFilter('use_state', 'new') 

, and this is because 'new' is not the value to filter by, but another, coded value, which is in my case, 234. I don't know how to translate 'new' to 234 though.

1
  • I have just found out that this line might be the culprit: ->addAttributeToFilter('use_state', 'new') , and this is because 'new' is not the value to filter by, but another, coded value, which is in my case, 234. I don't know how to translate 'new' to 234 though. Nov 6, 2016 at 20:25

3 Answers 3

1
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('name');  
$collection->addAttributeToSelect('price');    

//products whose price is 100

$collection->addFieldToFilter(array(
    array('attribute'=>'price','100'),
));
0

I have just found out that this line is the culprit:

->addAttributeToFilter('use_state', 'new')

, and this is because 'new' is not the value to filter by, but another, coded value, which is in my case, 234. I don't know how to translate 'new' to 234 though.

0

For Magento 2.3.4 you can use this:

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class SomeClass
{

    protected $collectionFactory;

    public function __construct(
        CollectionFactory $collectionFactory,
    ) {
        $this->collectionFactory = $collectionFactory;
    }

    public function something() 
    {
        $productCollection = $this->collectionFactory
                    ->create()
                    ->addFieldToFilter('attribute_code', 'attribute_value');
    }
}

attribute_code you can find in the eav_attribute table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.