Default attribute value for all product in magento

2020-02-11 09:08发布

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

4条回答
仙女界的扛把子
2楼-- · 2020-02-11 09:47

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楼-- · 2020-02-11 09:50

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.

查看更多
趁早两清
4楼-- · 2020-02-11 09:56

You can do it in Attribute management

Admin panel - Catalog - Attributes - Manage Attributes

Select attribute - Properties - Attribute Properties - Default value

enter image description here

查看更多
Summer. ? 凉城
5楼-- · 2020-02-11 10:09

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);
查看更多
登录 后发表回答