Magento – Update Custom Attribute Value Programatically

custom-attributesimportproduct-attributeprogrammatically

I have created a script that tests importing products programmatically into Magento. Unfortunately, I'm having trouble updating a product's custom attribute value – in this case "manufacturer." When I run the below code, I receive a fatal error with method "Invalid method." Does anyone know what I'm doing wrong?

<?php 
require_once('app/Mage.php');
ini_set('display_errors', 1);

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    $product = Mage::getModel('catalog/product');
    $rand = rand(1, 9999);
    $product
    ->setTypeId('simple')
    ->setAttributeSetId(4) // default attribute set
    ->setSku('example_sku' . $rand) // generate a random SKU
    ->setWebsiteIDs(array(1))
    ->setUrlKey('test-product-123') // set url key
    ;

$product
    ->setCategoryIds(array(3,4))
    ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) // visible in catalog and search
    ;

$product->setStockData(array(
        'use_config_manage_stock' => 0, // use global config?
        'manage_stock'            => 1, // should we manage stock or not?
        'is_in_stock'             => 1,
        'qty'                     => 5,
    ));

$product
    ->setName('Test Product #' . $rand) // add string attribute
    ->setShortDescription('Description') // add text attribute

    // set up prices
    ->setPrice(24.50)
    ->setSpecialPrice(19.99)
    ->setTaxClassId(2)    // Taxable Goods by default
    ->setWeight(87)
    ;

$optionId = Mage::getModel('eav/entity_attribute')->_getAttributeCode('manufacturer', 'PATAGONIA');
$product->setManufactuer($optionId);

$product->save();

Best Answer

I was able to solve my question and update my "manufacturer" attribute using the following code:

$attrCode = 'manufacturer';
$valueText = ('PATAGONIA');
$valueId = Mage::getModel('catalog/product')->getResource()->getAttribute($attrCode)->getSource()->getOptionId($valueText);
$product->setData($attrCode, $valueId);