1

When I create product programmatically I want to set custom attributes value. try to below code but it displays

Uncaught Error: Call to a member function setCustomAttribute()

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku('sku');
$product->setName('Sample Product');
$product->setAttributeSetId(4);
$product->setStatus(1);
$product->setWeight(10);
$product->setVisibility(4);
$product->setTaxClassId(0);
$product->setTypeId('simple');
$product->setPrice(100);
$product->setStockData(
                        array(
                            'use_config_manage_stock' => 0,
                            'manage_stock' => 1,
                            'is_in_stock' => 1,
                            'qty' => 999999999
                        )
                    );
$product->setCustomAttribute('custom_attribute','Attributevalue');
$product->save();
1

You can try below code. where CustomAttribute('custom_attribute') is your attribute code.

$product->setCustomAttribute('Attributevalue');
$product->save();

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.