开发者

Default attribute value for all product in magento

I want to set a default value of an a开发者_如何学编程ttribute for all product.


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


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


Default attribute value for all product in magento

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.


You can do it in Attribute management

Admin panel - Catalog - Attributes - Manage Attributes

Select attribute - Properties - Attribute Properties - Default value

Default attribute value for all product in magento

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜