Magento – Set default value to custom attribute for all products

collection;default valuesmagento-1product-attributeproduct-collection

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.

Best Answer

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
);
Related Topic