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();
Rizwan Khan
  • 1,939
  • 2
  • 17
  • 40
Sanket Makavana
  • 340
  • 2
  • 15
  • Please see if this link is helpful https://magento.stackexchange.com/questions/192160/magento-2-set-custom-product-attribute-value-programmatically-on-product-save – Raj Mohan R Apr 04 '19 at 13:34

1 Answers1

1

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

$product->setCustomAttribute('Attributevalue');
$product->save();
Mohit chauhan
  • 840
  • 5
  • 15