Create a product from PHP - Magento

Create a product from PHP - Magento

August 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Create a product from PHP - Magento

Magento Product Creation: A Comprehensive Guide for Developers

Creating products programmatically is a crucial aspect of managing large-scale e-commerce websites built on Magento. While the platform provides robust APIs, creating products efficiently requires a solid understanding of the underlying data structure and the proper use of classes and methods.

My friend recently faced a challenge when setting up his Magento webshop with thousands of products. The manufacturer was unable to provide an import file, but allowed the use of product information available on their website. To streamline this process without resorting to direct SQL queries, he decided to create a PHP script that leverages the Magento API.

Understanding the Magento Product Creation Process

In Magento, creating a new product involves several steps:

  1. Initialize the Magento application.
  2. Create an instance of the catalog/product model.
  3. Set the basic attributes like name and description.
  4. Configure stock data and tax settings.
  5. Specify website IDs, category IDs, status, and visibility.
  6. Add images to the product gallery.

The Initial Script: A Closer Look

The provided script is a good starting point for creating products programmatically in Magento. It follows the outlined steps and demonstrates some essential practices:

  • Error reporting is enabled (error_reporting(E_ALL | E_STRICT); ini_set('display_errors', 1);) to capture any issues that arise during execution.
  • The Magento application is initialized using Mage::init();.
  • A new product instance is created using Mage::getModel('catalog/product');.

However, the script encounters an exception when attempting to save the product:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`catalog_product_entity`, CONSTRAINT `FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID` FOREIGN KEY (`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_id`) ON DE)

This integrity constraint violation occurs because the script is attempting to save a product without specifying an attribute set ID that exists in the eav_attribute_set table.

Resolving the Integrity Constraint Violation

To resolve this issue, you need to ensure that the attribute_set_id used for the product actually exists in the eav_attribute_set table. Here are a few options:

  1. Use an Existing Attribute Set ID: Magento 2 comes with several default attribute sets (like “Default”). You can retrieve an existing attribute set using the following code:
$attributeSet = Mage::getModel('catalog/product')->getResource()->getAttributeSetIdByCode('default');

Then, use $product->setAttributeSetId($attributeSet);.

  1. Create a New Attribute Set: If you need to create a new attribute set, you can do so using the following script:
$attributeSet = Mage::getModel('eav/entity_attribute_set')->setEntityTypeId(4)->setAttributeSetName('New Attribute Set')->save();

Then, use $product->setAttributeSetId($attributeSet->getId());.

  1. Use a Specific Attribute Set: If the manufacturer provides specific attribute set information, you can directly use that ID.

Best Practices for Magento Product Creation

When creating products programmatically in Magento:

  • Always initialize the Magento application using Mage::init(); to ensure the correct environment is loaded.
  • Use appropriate error reporting settings for debugging purposes.
  • Handle exceptions gracefully to prevent data corruption or application crashes.
  • Ensure that all required dependencies and foreign key constraints are properly managed before saving entities.

Conclusion

Creating products programmatically in Magento requires a deep understanding of the platform’s architecture and APIs. By following best practices, you can efficiently manage large-scale product inventories without resorting to direct SQL queries.

For more in-depth guides on Magento development and best practices, I recommend exploring our comprehensive resources at Laravel Company. Our expert team provides detailed tutorials, code examples, and real-world scenarios to help you master Magento development effectively.

Catalog Database API Configuration

Our network