Hyvä Theme is Now Open Source: What This Means for Magento Community - Mageplaza
Hyvä is now Open Source and free. Discover what changed, what remains commercial, how it impacts the Magento ecosystem, and how to maximize its full potential.
Vinh Jacker | 03-17-2025
Creating products programmatically in Magento 2 can be a time-saver, especially if you’re dealing with bulk product imports or automating the catalog management process. In this article, we will walk through how to create a simple product programmatically in Magento 2.
This method is particularly useful for developers or store owners who want to automate product creation without manually entering each product into the admin panel.
A simple product in Magento 2 is the most basic type of product you can create. It doesn’t have any variations (like size or color) and is typically used for products that are sold individually without any custom options. Examples include single items like books, electronics, or clothing.
Creating products programmatically can save time and reduce the risk of human error. This is especially useful when dealing with a large product catalog or when integrating Magento 2 with external systems, such as ERPs or custom inventory management systems. Automating product creation can also streamline tasks such as data migrations or batch imports.
Before creating products programmatically, ensure you have:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku('my-sku'); // Set your sku here
$product->setName('Sample Simple Product'); // Name of Product
$product->setAttributeSetId(4); // Attribute set id
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$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(100); // price of product
$product->setStockData(
array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => 1,
'qty' => 999999999
)
);
$product->save();
You can add multiple images into media gallary of this product.
// Adding Image to product
$imagePath = "sample.jpg"; // path of the image
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();
This can be applied for other product type such as Virtual Products, Downloadable Product, etc.
// Adding Custom option to product
$options = array(
array(
"sort_order" => 1,
"title" => "Custom Option 1",
"price_type" => "fixed",
"price" => "10",
"type" => "field",
"is_require" => 0
),
array(
"sort_order" => 2,
"title" => "Custom Option 2",
"price_type" => "fixed",
"price" => "20",
"type" => "field",
"is_require" => 0
)
);
foreach ($options as $arrayOption) {
$product->setHasOptions(1);
$product->getResource()->save($product);
$option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($arrayOption);
$option->save();
$product->addOption($option);
}
With this script code, you should take some notes to create simple, configurable, downloadable products like the following:
This is a tip to help you create configurable products. Please using the snippet below:
$productId = 12; // Configurable Product Id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product
$attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
$position = 0;
$attributes = array(134, 135); // Super Attribute Ids Used To Create Configurable Product
$associatedProductIds = array(2,4,5,6); //Product Ids Of Associated Products
foreach ($attributes as $attributeId) {
$data = array('attribute_id' => $attributeId, 'product_id' => $productId, 'position' => $position);
$position++;
$attributeModel->setData($data)->save();
}
$product->setTypeId("configurable"); // Setting Product Type As Configurable
$product->setAffectConfigurableProductAttributes(4);
$objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $product);
$product->setNewVariationsAttributeSetId(4); // Setting Attribute Set Id
$product->setAssociatedProductIds($associatedProductIds);// Setting Associated Products
$product->setCanSaveConfigurableAttributes(true);
$product->save();
If there is the same assignment (the associated product is already assigned to configurable product), the command will run error.
Programmatically creating simple products in Magento 2 can streamline your catalog management and save time, especially for larger stores. By using the steps outlined in this guide, you can automate product creation and avoid the tedious process of manually entering products into your store.
Whether you’re creating one product or hundreds, this method offers a flexible way to manage your Magento 2 store efficiently. Always remember to test your script thoroughly in a development environment before applying it to a live store.
Related Topics