Magento 1.9.X add product with custom options to cart from external site

Magento 1.9.X add product with custom options to cart from external site

September 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 1.9.X add product with custom options to cart from external site

Integrating Custom Products with Magento 1.9.X Cart

As an experienced developer, I understand your challenge in synchronizing data between an external website and your Magento 1.9.X cart. In this post, I’ll guide you through the process of adding a custom product with options to a customer’s shopping cart from outside Magento.

Understanding Magento’s Security Changes

With Magento 1.9.x, the platform has tightened security measures, making it impossible to directly use URL redirection for adding products to the cart as you attempted in your example. This change prevents malicious third-party sites from manipulating customers’ carts.

Triggering Events and Observers from Outside Magento

To achieve this functionality, we’ll need to leverage Magento’s event-driven architecture. The key lies in triggering an event that adds a product with custom options to the cart.

Step 1: Define Your Custom Event

Create a new XML file named your_module.xml under the app/code/local/YourCompany/YourModule/etc/events.xml. Define your custom event:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="your_custom_event_name">
        <observer name="your_module_observer" instance="YourCompany\YourModule\Observer\YourCustomObserver"/>
    </event>
</config>

Replace your_custom_event_name with a unique identifier for your event, and YourCompany/YourModule/Observer/YourCustomObserver with the path to your observer class.

Step 2: Create Your Observer

Create the observer class in the specified directory:

<?php
namespace YourCompany\YourModule\Observer;

class YourCustomObserver implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Add your code here to add product with custom options to cart
    }
}

Step 3: Dispatching the Event from Outside Magento

To dispatch this event, you can use the Mage::dispatchEvent() function. However, since you’re working outside of Magento, this isn’t directly possible.

An alternative approach is to create a simple PHP script (not part of your Magento setup) that sends an HTTP POST request with the required data to trigger the event within your Magento instance.

For example:

$url = 'http://your_magento_instance.com/index.php/your_module/event/dispatch/';
$data = array(
    'event' => 'your_custom_event_name',
    'arg1'  => $product_id,
    'arg2'  => $option1_value,
);
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

Step 4: Handling the Event Within Magento

In your observer class (YourCustomObserver), you can now add the product with custom options to the customer’s cart using Magento’s core functions.

For instance:

$cart = $observer->getEvent()->getCart();
$productId = (int) $observer->getEvent()->getArg1(); // Extract the product ID from event arguments

$options = array(
    'option_id' => 1,
    'option_value' => $observer->getEvent()->getArg2(),
);

$cartItem = $cart->addProduct($productId, 1);
$cartItem->addOption($options);

Conclusion

By following this approach, you can successfully add a custom product with options to the customer’s Magento cart from an external website. Remember to thoroughly test your implementation and validate that the data is being transferred securely.

For more detailed guidance on Magento events and observers, I recommend checking out our comprehensive tutorials at LaravelCompany.

This method ensures you adhere to Magento’s security standards while allowing seamless integration between your external site and the customer’s shopping cart.

Magento 1 Checkout Catalog Security

Our network