Magento. How to add simple product in cart programmatically with custom options

Magento. How to add simple product in cart programmatically with custom options

September 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento. How to add simple product in cart programmatically with custom options

Magento: Programmatically Add Simple Product to Cart with Custom Options

Are you struggling to add a simple product with custom options to your Magento cart programmatically? You’re not alone! In this post, we’ll dive into resolving the “Please specify the product required option(s)” error and guide you through adding products correctly.

Understanding the Error: Required Options Missing

The error message you encountered—“Please specify the product required option(s)“—implies that your code is missing the necessary custom option data for the specified product. Magento requires this information to proceed with the cart addition.

Let’s break down your current approach and identify where things went wrong:

$arrProducts = array(
    array(
        "product_id" => 8,
        "qty" => 1,
        "options" => array(
            '1' => array(
                'sku' => 'cheese'
            )
        )
    )
);

While you’ve defined the product_id and qty, the issue lies in how you’re specifying the custom options. Magento needs more information than just the option ID and SKU.

Correctly Defining Custom Options for Cart Addition

To add a simple product with custom options to the cart, you need to provide complete and accurate option data:

  1. Option IDs: Use the actual option IDs from your Magento admin.
  2. Option Values: Include the correct value IDs or codes corresponding to each option.

Here’s an updated code snippet that demonstrates how to structure your options array correctly:

$arrProducts = array(
    array(
        "product_id" => 8, // Product ID (integer)
        "qty" => 1, // Quantity (integer)

        // Correctly define custom options
        "options" => array(
            '1' => array( // Option ID (integer)
                'option_id' => 1, // Option ID from Magento admin
                'option_value' => array( // Option value(s) as an array
                    'option_id' => 1,
                    'value_id' => 2 // Value ID corresponding to the option
                )
            ),
            '2' => array(
                'option_id' => 2, // Another option ID from Magento admin
                'option_value' => array(
                    'option_id' => 2,
                    'value_id' => 3 // Corresponding value ID
                )
            )
        )
    )
);

Complete Code for Adding Product to Cart

Now that you have the correct options format, replace your previous code with this:

$cart = Mage::getSingleton('checkout/cart');

// Add product(s) to cart with correct option data
$cart->addProduct($_product, $arrProducts);

// Save the cart
$cart->save();

// Ensure the cart was updated
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Best Practices and Additional Resources

  1. Always use complete and accurate option IDs and value IDs to avoid errors.
  2. Double-check your option configurations in the Magento admin to ensure they match your code.

For more advanced scenarios, such as adding custom options with multiple values or applying special pricing rules, consider exploring our comprehensive Magento tutorial series for step-by-step guidance.

Don’t forget to keep your Magento installation up-to-date for the best performance and security. Learn more about Magento updates and version compatibility.

Conclusion

Adding simple products with custom options to the Magento cart programmatically requires a precise definition of option data. By providing accurate option IDs and value IDs, you can successfully add products without encountering errors related to missing required options.

Remember that understanding your product configuration in the Magento admin is crucial for writing correct code. Always double-check your option settings to ensure they match your development approach.

Happy coding, and don’t hesitate to explore our Magento tutorials for more advanced topics!

Checkout Catalog Admin Errors

Our network