Posted on

magento 2.4 programmatically create new product with custom id

the usual procedure to create a new product via code is to call the product factory, set the new data, then save using the repository:

$data = [
‘id’ => $id,
‘sku’ => $sku,
‘type’ => MagentoCatalogModelProductType::TYPE_SIMPLE,
… // a bunch of other product data
$product = $this->productInterfaceFactory->create();
$product = $this->productRepositoryInterface->save($product);

This code works but the $data[‘id’] that I pass is overridden and magento sets its own. Is there a way to save the product with the id that I pass?

Leave a Reply

Your email address will not be published.