Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Questions on using addAttributeToSelect();
 
nick_gc
Jr. Member
 
Total Posts:  7
Joined:  2009-03-04
 

Hello,

I’m trying to modify the gbase.php posted here to feed products to Yahoo Shopping, but Yahoo doesn’t like products that lack a price (like grouped products, for example), so I’m trying to select only products that are simple products, not grouped products, and I’ve been trying to do this here:

$products Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status'1);//enabled
$products->addAttributeToFilter('visibility'4);//catalog, search
$products->addAttributeToSelect('type_id''simple'); //<-------------Here
$products->addAttributeToSelect('*');
$prodIds=$products->getAllIds();

I’ve tried this as well as looking for product_type (1-4), etc. Am I completely misunderstanding how to use addAttributeToSelect? Thank you for any help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
benz001
Sr. Member
 
Avatar
Total Posts:  238
Joined:  2009-02-05
Sydney
 

Better late than never - for anyone reading this, use addAttributeToFilter to get simple products not addAttributeToSelect, the select controls what entity attributes you will return in your collection not which entities you will get.

So something like this:

$products Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status'1);//enabled
$products->addAttributeToFilter('visibility'4);//catalog, search
$products->addAttributeToFilter('type_id''simple'); //<-------------Changed Here
$products->addAttributeToSelect('*');
$products->load();
$prodIds=$products->getAllIds(); 
Zend_Debug::dump($prodIds); // dump it to the page for testing

If you are trying to get just a list of entity attributes to display, rather than all of them, pass the list as an array like this

$addresses->addAttributeToSelect(array('company','region','country_id','city','postcode'))
 
Magento Community Magento Community
Magento Community
Magento Community
 
oscarino
Jr. Member
 
Total Posts:  22
Joined:  2010-05-23
 

Hallo
thanks for the script.
I can’t understand how can I echo data such as:
- sku, title, descirption, image… , .... , attributes, ecc.

Maybe there is a reference for addAttributeToSelect() ?
I was not able to find it,
Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Moderator
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 
oscarino - 24 July 2010 02:31 PM

Hallo
thanks for the script.
I can’t understand how can I echo data such as:
- sku, title, descirption, image… , .... , attributes, ecc.

Maybe there is a reference for addAttributeToSelect() ?
I was not able to find it,
Thanks

//using methods
$product->getSku();
$product->getDescription();

//using properties
$product->sku;
$product->description;

//using array
$data $product->getData();
$sku $data['sku'];
$description $data['description'];

Hope this helped a bit.
Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
oscarino
Jr. Member
 
Total Posts:  22
Joined:  2010-05-23
 

Thanks, it really helped me.
I am new to Magento.
You and all the people from the forum are really great.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cmpreshn
Jr. Member
 
Total Posts:  1
Joined:  2012-04-03
 

First of all, great post - thanks everyone for the info.

I’ve got a customer collection I’m working on now, and I’m able to select and filter with:

$collection -> addAttributeToSelect(array('field1','field2')) ->
addAttributeToFilter('group_id',3);

Does anyone know the method sort a collection?

Specifically, I’ve got a list of customers, filtered by group_id, that I would like to sort alphabetically by last name. So I’m assuming there must be a method that would be the equivalent of adding ‘order by’ to a collection query.

Any help appreciated!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top