0

I am creating simple products and try to set some attribute values

$attributeSetId = $product->getDefaultAttributeSetId();
$product->setSku($sku); // Set your sku here
$product->setName($product_title); // Name of Product
$product->setAttributeSetId($attributeSetId); // Attribute set id
$product->setStatus(0); // Status on product enabled/ disabled 1/0, disabled by default

$product->setWeight(10); // weight of product   

$product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(0); // Tax class id
$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice($product_price); // price of product
$product->setStockData(
    array(
        'use_config_manage_stock' => 0,
        'manage_stock' => 1,
        'is_in_stock' => 1,
        'qty' => $qtyAvailable
    )
);
echo "Qty: ", $qtyAvailable, "\n";
foreach ($options_list as $current_option) {
    $attributeCode = $current_option["attribute_code"];
    $attributeValue = $current_option["option_code"];
    if ($attributeCode == 'color') {
            $product->setColor($attributeValue);
            echo "Color ";
    } else {
            $product->setData($attributeCode, $attributeValue);
    }
    echo "Setting attribute: ", $attributeCode, " ", $attributeValue."\n";
}
$product->save();

I have created 2 product attributes in the backend, color and shipsfrom. I have added these attributes to the default set.

As you can see in the code I am trying different methods to add the attribute value to my product, setColor or setData.

The values in my options array are as follow:

attributeCode    attributeValue
color            White
shipsfrom        Spain

However neither color nor shipsfrom got any value assigned to them after product creation, while all other standard attributes do get values assigned properly.

Am I using the wrong methods to update these attributes ?

| improve this question | | | | |
0

After many try attempts I finally made it work:

setData was not working with option code, but was with option ID. I modified the code as following:

foreach ($options_list as $current_option) {
        $attributeCode = $current_option["attribute_code"];
        $attributeValue = $current_option["option_code"];
        $attr = $product->getResource()->getAttribute($attributeCode);
        $value = $attr->getSource()->getOptionId($attributeValue);
        $product->setData((string)$attributeCode, $value);
        echo 'Setting ', $attributeCode, ' to ', $attributeValue, "\n";
    }

Also important to note, $product-save(); is not the right way anyore in magento, instead you should use the productRepository:

$productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');
...
$product = $productRepository->save($product);
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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