Default attribute value for all product in magento

2020-02-11 09:46发布

问题:

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

回答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);


回答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();


回答3:

You can do it in Attribute management

Admin panel - Catalog - Attributes - Manage Attributes

Select attribute - Properties - Attribute Properties - Default value



回答4:

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.