0
votes

I made a simple import script and I'm trying to programatically save 3 custom attributes (att1, att2, att3) together with all other info (name, description, price, category..).

So basically I have:

public function insert_product($data) {

    $product = Mage::getModel('catalog/product');   

    try {

        $sku = $data['code'];

        if ($this->prodottiImportati[$sku]) {

            $sku = $data['code'] . '-1';        
        }
        $this->prodottiImportati[$sku] = true;

        $product->setSku($sku);
        $product->setName($data['name']);
        $product->setDescription($data['desc']);
        $product->setShortDescription($data['short_desc']);
        $product->setManufacturer('');
        $product->setPrice($data['price']);

        $product->setTypeId('simple');
        $product->setAttributeSetId($this->attributeSet);

        $categorie = $this->get_categories($data);
        $product->setCategoryIds($categorie);

        $product->setWeight($data['peso']);

        $product->setTaxClassId(2); // taxable goods
        $product->setVisibility(4); // catalog, search
        $product->setStatus(1); // enabled

        $product->setWebsiteIds($data['store_id']);

        $stockData = $product->getStockData();
        $stockData['qty'] = $data['qty'];
        if ($data['quantita'] > 0) {
            $stockData['is_in_stock'] = 1;  
        } else {
            $stockData['is_in_stock'] = 0;
        }
        $stockData['manage_stock'] = 1;
        $stockData['use_config_manage_stock'] = 0;
        $product->setStockData($stockData);

        $product->setIsMassupdate(true)->setExcludeUrlRewrite(true);                
        $product->save();

        $productID = $product->getId();

    } catch(Exception $e) {

        echo ($e->getMessage());
    }

    return $productID;
}

First thing I tryed was adding a

$productID = $this->insert_product($data);
Mage::getSingleton('catalog/product_action')->updateAttributes(
            array($productID), array(
               'att1' => $data['att1'],
            ), $data['store_id']);

So basically updating things after the insert function was called, using the ID got after the insert. store_id is the ID of the store in that given language. Didn't save anything.

Second attempt, I follwed this: Magento add custom options with programmatically importing products I tryed that within the insert_product function and also outside after $productID = $this->insert_product($data); Neither worked.

Last I tryed a magical $product->setAtt1('value'); witin the insert_product function, not sure how Magento would understand how to set att1 that way, but...you know, I read it somewhere and I gave it a try ;)

att1, att2 and att3 are spelled lowercase, althoug they have an uppercase label (think that dosen't matter here), they are part of an attribute group (I'm passing it with $product->setAttributeSetId($this->setAttributi)) and they are all multiple selection attributes, so I could in teory pass multiple values to them.

I'm sure I'm missing something on the way. Can anyone help?

1

1 Answers

0
votes

After 10 more minutes since I wrote here, I was able to find the way. I took me forever to solve it.

The clue of this is that you have to add attributes ID, not values. That happens at least for me with multiple selection attributes, not sure if it's true all the time.

Here is how I did:

In the function insert_product I added:

$optionId = $this->get_option_id_by_code('att1', 'Value of the attribute you need to add'); 
$product->setAtt1($optionId);

So if yor attribute is named, let's say "brand" it will be:

$optionId = $this->get_option_id_by_code('brand', 'Nike'); 
$product->setBrand($optionId);

If your attribute can have multiple values, you need to change the code above to:

$optionId = array();
foreach ($myAttributesArray as $someValues) {

     $optionId[] = $this->get_option_id_by_code('att1', $someValues);
}
$product->setAtt1($optionId);

The foreach is just an example, you need to loop through your mutiple values and get the respective ID and add them all toghether with setAtt1 passing them as an array. I'm working on an improved version where the get_option_id_by_code function does all at once in a more efficient way. This is kust a "basic" version that works, feel free to make it fancy and smart.

Then I created an other function called get_option_id_by_code like this:

protected function get_option_id_by_code($attrCode, $optionLabel) {

    $attrModel   = Mage::getModel('eav/entity_attribute');

    $attrID      = $attrModel->getIdByCode('catalog_product', $attrCode);
    $attribute   = $attrModel->load($attrID);

    $options     = Mage::getModel('eav/entity_attribute_source_table')
        ->setAttribute($attribute)
        ->getAllOptions(false);

    foreach ($options as $option) {
        if ($option['label'] == $optionLabel) {
            return $option['value'];
        }
    }

    return false;
}

To be honest I found this with a collage of other sources / authors, so I need to be thankful to a bunch of smarter programmers here and there, since it took a while for me to strouggle with this simple task I wrote the solution here hoping to help you guys. Thanks!