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 ?