davidselo
davidselo

Reputation: 1325

Magento- can´t retrieve custom attribute of collection added in the product Model

I don´t get to understand the complety behavior os the collection in Magento. next, I´ll describe the problem:

For requirements of the projects, I need to add a custom attribute to exclude a severals products in the feed at GoogleShopping. Then I add this attribute with php script installation

<?php
  $installer = $this;
  $installer->startSetup();
  $installer->addAttribute('catalog_product', 'in_googleshopping_feed', array(
    'group'                    => 'General',
    'type'                     => 'int',
    'input'                    => 'select',
    'label'                    => 'In GoogleShoppint feed',
    'global'                   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'                  => 1,
    'required'                 => 0,
    'default'          => 1,
    'visible_on_front'         => 0,
    'is_html_allowed_on_front' => 0,
    'sort_order'               => 32,
    'is_configurable'          => 0,
    'source'                   => 'eav/entity_attribute_source_boolean',
    'searchable'               => 0,
    'filterable'               => 0,
    'comparable'               => 0,
    'unique'                   => false,
    'user_defined'             => false,
    'is_user_defined'          => false,
    'used_in_product_listing'  => true
  )
  );


  $installer->endSetup();

Next in a Observer I try retrieve the value of this with:

$products = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
            //->addAttributeToSelect('in_googleshopping_feed');
        ->addAttributeToFilter('in_googleshopping_feed',0);

This is my doubt, Why haven´t the collection this attribute?

However, I can retrieve the value of the Product_Model whit the next snippet:

$products = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('*');
$prodIds=$products->getAllIds();
foreach($prodIds as $productId):
   $product = Mage::getModel('catalog/product')->setStoreId('1');
   $product->load($productId);
   var_dump($product->getData('in_googleshopping_feed'));
endforeach;

Then, My huge doubt is: Why I can´t filter the collection by my new attribute? I think with the method addAttributeToSelect('*') all fields are added to the collection.

Can someone help my? Thanks

Upvotes: 1

Views: 2226

Answers (1)

Tim Bezhashvyly
Tim Bezhashvyly

Reputation: 9100

The first that falls into glance is that 2nd parameter for addAttributeToFilter have to be array. Like this:

addAttributeToFilter('in_googleshopping_feed', array('eq', 0));

Upvotes: 2

Related Questions