I am trying to create and update products in the global scope while making sure any attributes are set to “use default” values set in global scope. The script is triggered by CRON so there is no store/website value set like there is when accessing via web browser in index.php.
The product gets created, but the product attributes “use default” are unchecked in store view scope.
The following code snippets are how I create a new product, how I get and update an existing product, and how I save the products data.
Create Product Code
$product = $this->productFactory->create();
$product->setStoreId(0)
->setSku("my-sku")
->setName("Product name")
->setStatus(1)
->setVisibility(4)
->setTypeId("simple")
->setAttributeSetId(4)
->setUrlKey($product->formatUrlKey("my-sku"))
->setCustomAttribute('short_description', "This is a short description of the product");
/* Plus I set other attributes such as price, name, meta etc */
Get Product Code
$product = $this->productRepository->get("my-sku", true, 0, false);
$product->setCustomAttribute('short_description', "This is the NEW UPDATED short description of the product");
Save Product
$this->productRepository->save($product);
The “use default” checkbox is unchecked for any/all attributes that have their scope not set to Global in the attribute setup.
My question is: How do I programmatically create and update products in the global scope while making sure any other scopes the attribute may have, use the default value?