cancel
Showing results for 
Search instead for 
Did you mean: 

Programmatically Assign Attributes to Products

Programmatically Assign Attributes to Products

Hello everyone,

 

I am performing a mass product import and have automation scripts to handle all aspects of imports besides attribute assignments. I've spent many hours looking for resources for a solution and can't seem to find one, even through programming my own. So far I am successfully altering the product object but it's not actually saving or retaining the changes. It seems I'm finding a lot of contradicting information out there on how to go about it as well. Has anyone successfully written a script to accomplish this?

 

// This is my latest attempt. All variables are populated ($attributeName for example)

$product = Mage::getModel('catalog/product');
$product = Mage::getModel('catalog/product')->setCurrentStore(1)->load($product->getIdBySku($sku));
$product->getResource()->addAttributeUpdate($attributeName, $value, 0);

$product->getResource()->addAttributeUpdate($attributeName, $value, 0);
$product->save();

 

I've also been trying the following, once having the product and attribute objects similar to how I instantiated them earlier:

$product->addAttributeUpdate($attributeName, $value, 0);

$product->setData('diameter', '10.5');
$product->getResource()->saveAttribute($product, 'diameter', '2');

I'm really quite baffled at this point. Anyone with any suggestions? They'd be much appreciated. Thanks for your time!

 

- Bobby