Default attribute value for all product in magento

19,836

Solution 1

I had same problem before,when i added 11096 product(downloadable products) in my store then client told me to add new attributes in product so i create 1 attribute (Type is Yes/No) and set to attribute set. Now my problem is how can i edit all product and set that attribute yes or not.if i not set then value is null so i wrote few line code.

Please check this code may be it'll helpful to you.

$ProductId = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('type_id', Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE)
    ->getAllIds();
//Now create an array of attribute_code => values

$attributeData = array("my_attribute_code" =>"my_attribute_value");

//Set the store to affect. I used admin to change all default values

$storeId = 0; 

//Now Update the attribute for the given products.

Mage::getSingleton('catalog/product_action')
    ->updateAttributes($ProductId, $attributeData, $storeId);

Solution 2

As stated here:- http://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-attributes-custom-fields

Default Value: You can enter a value that will automatically populate for new products.

So, I think it only applies for New products that you create after you create that attribute. Hence, the attribute's default value setting might not be applied to those products which were created before creating the attribute.

I had the similar issue and to solve it, I wrote the following code in the file where I want to display the attribute's default value:-

$attributeCode = 'YOUR_ATTRIBUTE_CODE';
$attribute = Mage::getResourceModel('eav/entity_attribute_collection')
            ->setCodeFilter($attributeCode)
            ->getFirstItem();
echo $attribute->getDefaultValue();

Solution 3

enter image description here

You can also solve the problem by doing a massupdate for all products. Go to the Manage Products paga and Select All followed by Update attributes.

Share:
19,836
Shawkat Alam
Author by

Shawkat Alam

i am working in bluehorse software company. i am working with magento. i am new in it. i want to become a magento champ.

Updated on June 04, 2022

Comments

  • Shawkat Alam
    Shawkat Alam almost 2 years

    I want to set a default value of an attribute for all product.

  • Shawkat Alam
    Shawkat Alam about 13 years
    can you tell me how it is possible with attribute management.
  • Shawkat Alam
    Shawkat Alam about 13 years
    my attribute is dropdown. i cant seeany default value option in attribute properties. sorry....but thanks for trying to help me...if there is another process then please tell
  • Distdev
    Distdev about 13 years
    For dropdown you can use Is Default option on Manage Label/Options tab
  • Shawkat Alam
    Shawkat Alam about 13 years
    actually i have done that but that is working for the products i have adding after that, but i want that for existing products. Help me please
  • Distdev
    Distdev about 13 years
    try to remove attribute from consistent attribute set, save and add it again
  • Shawkat Alam
    Shawkat Alam about 13 years
    i tried but not working. still not selected in existing products
  • Eric Seastrand
    Eric Seastrand over 8 years
    Be careful! That entity_attribute_collection contains attributes for other entities besides products (customers, categories, etc...). Make sure you filter by entity type too: $collection->setEntityTypeFilter(Mage::getModel('catalog/pro‌​duct')->getResource(‌​)->getTypeId())