7

I have created the custom attribute (test) for products as a text field with default value('test') from admin panel

And assigned that attribute to default attribute set.

Now I can able to see the new custom attribute in product edit page.

When I try to filter with the product collection

Mage::getModel('catalog/product')->getCollection()
       ->addAttributeToSelect('*')
       ->addAttributeToFilter('test', array('like' => 'test'))->getData();

It return the empty array.

My Question is :

Is there any way to set the default value to attributes through installer script?

Why the default value is not set to that attribute?

Is there is any way to add the default values for all products?. I have more than 10,000 products in my store.

Note :

I don't want load the whole product collection and set value for each product.

10

You could try something like this. I believe the below method is quite efficient.

$attrData = array(
    'attribute_code_here'=> 'Default Value Here',
);
$storeId = 0;
$productIds = Mage::getModel('catalog/product')->getCollection()->getAllIds();
Mage::getModel("catalog/product_action")->updateAttributes(
    $productIds, 
    $attrData, 
    $storeId
);
  • @marius Im a huge Fan. Thanks for the upvote :) – NathanielR Jun 18 '15 at 13:14
  • Is there is a way set this value in installer script or config.xml? – saravanavelu Jun 18 '15 at 13:18
  • @Marius I am building a new extension. So I will create this attribute through installer script. Is this possible to set this default value in installer scritp? – saravanavelu Jun 18 '15 at 13:35
  • @saravanavelu You could look at data scripts or create your attribute through the installer and then run the snippet as a standalone script – NathanielR Jun 18 '15 at 13:42
  • While installing the module, need to update the attributes is this possible without standalone script? – saravanavelu Jun 18 '15 at 13:45

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.