This is somewhat confusing.. I hope my explanation is clear.

As we all know magento converts a php object(I don't know what it is exactly) to an Array by using:

$item->getData();    // Where $item is an item from a product collection

The above code does covert the object to an Array, but I want to add my own custom fields to it.

So, I tried the following while preparing the collection

foreach($collection as $item){
    $item->setData('custom_field', $value);  // Also tried $item->setCustomField($value);

By doing the above, I can get the custom_field value by doing $item->getCustomField() or $item->getData('custom_field'); but I can't see this custom_field key in array when I am doing


For Example:

Let us say, If I do

echo $item->getData();

I can see the following array in dump:

array(4) {
  ["entity_id"] => string(3) "623"
  ["type_id"] => string(12) "configurable"
  ["created_at"] => string(19) "2012-07-30 22:14:09"
  ["updated_at"] => string(19) "2014-01-04 02:02:08"

But I want to see the following Array:

array(5) {
  ["entity_id"] => string(3) "623"
  ["type_id"] => string(12) "configurable"
  ["created_at"] => string(19) "2012-07-30 22:14:09"
  ["updated_at"] => string(19) "2014-01-04 02:02:08"
  ["custom_field"] => string(5) "Hello"
  • Maybe if you gave us an idea of what you are trying to achieve here, if you set the data and then in the loop call getData then it will be returned. – David Manners May 16 '14 at 15:30
  • Like David said, we're going to need a little more detail about what you're ultimately trying to do. Maybe you need to create a product attribute that will store the data you're trying to save? – andyjv Aug 12 '14 at 12:48
  • @andyjv I want to have my custom_field value in $item->getData(). Anyway, I got the answer below. (I just forgot to mark it) – Mr_Green Aug 12 '14 at 14:52

I think you just need to save your item , then you can use it :

       foreach($collection as $item){
            $item->setData('custom_field', $value);  // or $item->setCustomField($value);
var_dump($collection->getFirstItem()->getData()); // print first item
  • Thanks... Wouldn't that effect performance? because saving each item in the collection is I think doing db query for each item.. – Mr_Green May 13 '14 at 6:23
  • Yes, that's not good for performance . The best way is to add your custom field in your product collection before with $products = Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('custom_field'); (depends on how your custom field is stored) – Bio May 13 '14 at 6:27
  • How to do that? also I tried $item->save().. I still can't see my custom field.. – Mr_Green May 13 '14 at 6:29
  • $item->save() literally saves the item to the database, so if the fields aren't originally a part of the collection then it can't be saved because there is no spot in the database for it. – andyjv Aug 12 '14 at 12:46
  • Not a good performing ans, looping can make page too much slow to load. – amit_game Aug 20 '15 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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