Adding custom option to a product programatically in custom module frontend controller?

Adding custom option to a product programatically in custom module frontend controller?

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Adding custom option to a product programatically in custom module frontend controller?

Solving Magento Custom Option Insertion Errors

As an experienced Magento developer, I often encounter errors when trying to insert custom options for products programatically, especially from the frontend using a custom module controller. Let’s dive into the problem presented and find a practical solution.

Understanding the Current Approach

The developer is following the official Magento documentation on adding new products programmatically: https://docs.magento.com/m2/ce/user_guide/catalog/catalog-add-products.html

They have extended this code to include custom options for their product. However, they are facing an SQL constraint error when attempting to save the updated product.

The Foreign Key Constraint Violation Issue

The error message indicates a foreign key constraint failure involving catalog_product_entity and eav_attribute_set. This typically occurs when you’re trying to insert or update data that violates the relationship between these two tables in your database schema.

In this case, it seems the issue lies with the reference to the attribute_set_id field. Ensuring consistency between the product’s attribute set and its attributes is crucial for successful saving of a product.

A Step-by-Step Solution

To resolve this issue, we need to ensure that the product object is fully initialized before attempting to save it. Let me guide you through the necessary steps:

1. Initialize the Product Object Correctly

$product = Mage::getModel('catalog/product');

Ensure you have properly initialized the product model without any specific ID.

2. Set the Relevant Data

Set all the required data for your new product, including its name, description, price, etc.

3. Add the Custom Options

You are on the right track with adding custom options to the product:

$options = array();
$options[$sku] = array(
    'title' => 'Option Title',
    'type' => 'radio',
    'is_require' => 1,
    'sort_order' => 0,
    'values' => array()
);

However, ensure you are setting the values correctly:

$options[$sku]['values'][] = array(
    'title' => 'Option Value 1',
    'price' => 0.00,
    'price_type' => 'fixed',
    'sku' => '',
    'sort_order' => 1
);

$options[$sku]['values'][] = array(
    'title' => 'Option Value 2',
    'price' => 89.00,
    'price_type' => 'fixed',
    'sku' => '',
    'sort_order' => 1
);

4. Assign the Options to the Product

$product->setProductOptions(array($options));
$product->setCanSaveCustomOptions(true);

Ensure you are correctly assigning the options array to the product before saving.

5. Save the Product

Finally, save your fully initialized and populated product object:

try {
    $product->save();
} catch (Exception $e) {
    // Handle exception, e.g., log or re-throw
}

Best Practices for Magento Programmatic Operations

Remember these best practices when working with Magento programmatically:

  • Always initialize objects correctly.
  • Set all required data before saving an object.
  • Use try-catch blocks to handle exceptions gracefully.

If you’re looking for expert guidance on Magento development, feel free to explore our resources at Laravel Company. We provide comprehensive tutorials and professional services tailored for Magento developers like yourself.

Conclusion

The key to successfully adding custom options to a product programmatically is ensuring the product object is fully initialized with all necessary data before saving. Always check your database schema consistency when facing foreign key constraint violations.

Try re-implementing the solution provided above, following each step carefully. If you still encounter any issues or need further clarification, don’t hesitate to reach out to our Magento experts at Laravel Company. We are always ready to help you succeed in your Magento development projects.

Catalog Database Extensions Errors

Our network