Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Magento dropdown (select) value for product

I have created quite big import script which is importing products from CSV to magento. I have one remaining issue to resolve.

I use dropdowns for attributes. Unfortunately I can't set values for those attributes for a single product. What I did:

  • created attribute set [php],
  • added dropdown attribute with values to this set [php],
  • added new product in proper attribute set and tried to set value for attribute I have created.

I tried few methods, here is the one looking good for me:

private function setOrAddOptionAttribute($product, $arg_attribute, $arg_value) {
    $attribute_model = Mage::getModel('eav/entity_attribute');
    $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table');

    $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);

    $attribute = $attribute_model->load($attribute_code);

    $attribute_options_model->setAttribute($attribute);
    $options = $attribute_options_model->getAllOptions(false);

    // determine if this option exists
    $value_exists = false;
    foreach($options as $option) {
        if ($option['label'] == $arg_value) {
            $value_exists = true;
            break;
        }
    }

    // if this option does not exist, add it.
    if (!$value_exists) {
        $attribute->setData('option', array(
            'value' => array(
                'option' => array($arg_value,$arg_value)
            )
        ));
        $attribute->save();
    }


    $product->setData($arg_attribute, $arg_value);
    $product->save();
}

Unfortunately it don't work. Any ideas? I'm using Magento 1.7.0.2

like image 572
webrama.pl Avatar asked Oct 06 '13 19:10

webrama.pl


2 Answers

You can do it like this:

$attr_id = $this->attributeValueExists('manufacturer', 'Samsung');
$product->setData('manufacturer', $attr_id );
$product->save();

public function attributeValueExists($arg_attribute, $arg_value)
{
    $attribute_model        = Mage::getModel('eav/entity_attribute');
    $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;

    $attribute_code         = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
    $attribute              = $attribute_model->load($attribute_code);

    $attribute_table        = $attribute_options_model->setAttribute($attribute);
    $options                = $attribute_options_model->getAllOptions(false);

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

    return false;
}

Let me know if you have any questions.

Cordially.

like image 156
MageDevBrossard Avatar answered Nov 20 '22 09:11

MageDevBrossard


I rewrote the initial function so it works for text and select fields:

public function setOrAddOptionAttribute($product, $arg_attribute, $arg_value) {
    $attribute_model = Mage::getModel('eav/entity_attribute');
    $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table');

    $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
    $attribute = $attribute_model->load($attribute_code);
    $attributeType = $attribute->getFrontendInput();
    if ($attributeType == 'select') {

        $attribute_options_model->setAttribute($attribute);
        $options = $attribute_options_model->getAllOptions(false);

        // determine if this option exists
        $value_exists = false;
        foreach ($options as $option) {
            if ($option['label'] == $arg_value) {
                $value_exists = true;
                break;
            }
        }

        // if this option does not exist, add it.
        if (!$value_exists) {
            $attribute->setData('option', array(
                'value' => array(
                    'option' => array($arg_value, $arg_value)
                )
            ));
            $attribute->save();
            // Now get the option value for this newly created attribute value
            $_product = Mage::getModel('catalog/product');
            $attr = $_product->getResource()->getAttribute($arg_attribute);
            if ($attr->usesSource()) {
                $option['value'] = $attr->getSource()->getOptionId($arg_value);
            }

        }
        $product->setData($arg_attribute, $option['value']);
    } else {
        $product->setData($arg_attribute, $arg_value);
    }
}
like image 24
Ed de Tollenaer Avatar answered Nov 20 '22 10:11

Ed de Tollenaer