Magento add products programmatically and redirect to checkout

Magento add products programmatically and redirect to checkout

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento add products programmatically and redirect to checkout

Programmatically Adding Products to Magento Cart and Redirecting to Checkout

As an experienced developer working with e-commerce platforms like Magento, I understand that sometimes you need to add products directly from an external site. In this post, we’ll guide you through programmatically adding products to the Magento cart using a PHP file and then redirecting to the checkout page.

Understanding the Current Issue

You’ve already attempted to create a PHP script that receives HTTP POST data to add products directly to the Magento cart. However, even though your var_dump($cart) shows that the products are being added successfully, you’re still facing an empty cart when trying to access it.

Let’s dive deeper into the issue and find the solution step by step.

Analyzing the Current Approach

You’ve started with a basic PHP script using Magento’s core classes:

<?php
namespace Addtocart;

use Mage;

require __DIR__ . '/app/Mage.php';

Mage::app();

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

/** @var \Mage_Checkout_Model_Cart $cart */
$cart = Mage::getSingleton('checkout/cart');
$cart->init();

$errors = array();
foreach($data as $product) {
    try {
        $productid = (int)$product_model->getIdBySku($product['id']);

        // ... (rest of the code)
}

$cart->save();
$cart->getItems()->setQuote($cart->getQuote());
$cart->save();

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

if ($errors) {
    var_dump($errors);
    exit;
}

header('Location: /checkout/cart/');

The script appears to be using the correct Magento classes and methods, such as Mage_Catalog_Model_Product for retrieving product data and Mage_Checkout_Model_Cart for adding products to the cart. However, there seems to be a subtle issue with session management.

Identifying the Root Cause

The problem lies in how you’re initializing the Magento application and managing sessions between the Mage::app() and Mage::run() calls. In your current setup, it appears that the session is being changed or lost somewhere during this process.

To ensure that the session persists across these calls, we need to properly initialize the Magento environment with the correct session information.

To fix this issue, you should follow these steps:

  1. Ensure that you have a valid app/Mage.php file in your project root directory. If not, you may need to create one using the official Magento documentation as a reference: Mage.php - Magento 2.x Documentation

  2. When initializing the Magento application using Mage::app(), pass the appropriate session parameters, such as the session name and session path:

$sessionParams = array(
    'name' => Mage_Session_Model_Varien_Session::SESSION_NAME,
    'path' => '/',
);

Mage::app('default', $sessionParams);
  1. After adding products to the cart, make sure to save any changes and update the session:
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
  1. Finally, when redirecting to the checkout page, ensure that you’re using the Mage_Core_Controller_Varien_Action’s redirect() method instead of the PHP header() function:
$this->_redirect('/checkout/cart/');

Best Practices and Recommendations

For optimal performance and security when working with Magento programmatically, consider the following best practices:

  1. Always use the official Magento documentation as a reference for proper API usage: Magento 2.x Developer Documentation

  2. Use Magento’s provided session management classes and methods instead of relying on PHP sessions directly.

  3. Ensure that your Magento environment is properly configured, including the database settings, session parameters, and server variables.

  4. Test your script thoroughly in a development or staging environment before deploying it to a production site.

Conclusion

In this post, we’ve explored the intricacies of programmatically adding products to the Magento cart using PHP scripts and redirecting to the checkout page. We identified the root cause of the issue you were facing and provided a step-by-step solution to resolve it.

By following the recommended approach, properly initializing the Magento application with session parameters, and using Magento’s built-in session management classes, you should be able to successfully add products programmatically and redirect users to the checkout page without any issues.

Remember that working with e-commerce platforms like Magento requires a deep understanding of their architecture and proper implementation of best practices for optimal performance and security. Always refer to the official documentation and consider seeking professional assistance if needed.

Happy coding!

Checkout Catalog API Errors

Our network