1

After searching for examples using "magento add attribute to product programmatically" I keep getting copious results on adding or removing ATTRIBUTES programmatically.

I want to programmatically add attribute values to a specific product - both the product and the attribute (and options) will have already been created previously. Something like:

function addAttributeToProduct($product, $attribute, $option_value, $scope='global'){ ...

Where $product could be the object or could be the product SKU, $attribute could be the attribute object or the attribute name, etc. with $option_value.

How would I do this?

1 Answer 1

0

It should work something along these lines:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$prod = Mage::getModel('catalog/product');
// get the product ID from the sku
$productId = $prod->getIdBySku($product); // if $product is the SKU
// load the product
$productToEdit = $prod->load($productId);
// set the attributes
$productToEdit->setAttributeName(array($options)); // 'AttributeName' is the name of your attribute using CamelCase (eg testattribute = TestAttribute)
// save
$productToEdit->save();

To see how the attribute options should be formatted, use var_dump($productToEdit->getAttributeName())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.