Magento - Dynamic Products / Dynamic Pricing / Creating Products on the fly

Magento - Dynamic Products / Dynamic Pricing / Creating Products on the fly

August 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Dynamic Products / Dynamic Pricing / Creating Products on the fly

Magento - Dynamic Products / Dynamic Pricing / Creating Products on the Fly

Hello, developers! Today, we’re going to tackle an interesting challenge that many of us face when working with large-scale e-commerce platforms like Magento. The scenario is straightforward: you have a Magento store with over 400,000 products, each with its own variations and options. Hundreds of these products are added or removed daily on your master store (which runs on a custom shopping cart system using MSSQL). You’ve successfully configured Magento to create product pages dynamically on the fly.

The Challenge: Adding Products to Cart Without Physically Existing in Backend

The problem arises when you want to add these dynamically generated products to the shopping cart without them actually existing in the backend. Currently, you have one general template type product set up in the backend and are trying to use it as a base for all variations.

The Goal: Changing Product Price via Front End Without Backend Modification

The aim is to change the price of these dynamic products on the front end using HTML or PHP before posting them to the shopping cart. This should be done without modifying the price in the backend.

Our Approach: Utilizing Magento’s Core Cart and Catalog APIs

To achieve this, we’ll utilize Magento’s core catalog and checkout APIs. We’ll start by initializing a new cart instance and then manually add products to it with custom prices. Here’s how you can do it:

  1. First, make sure you have the following line in your app Bootstrap.php file:
include_once ("app/Mage.php");
umask(0);
Mage::init("default");
  1. Next, get the current Magento cart instance:
$cart = Mage::getSingleton('checkout/cart');
  1. Create a new catalog product model with custom data:
$product = Mage::getModel('catalog/product');

$product->setCustomPrice($_POST['custom_price']);
$product->setOriginalCustomPrice($_POST['custom_price']);

// Set other attributes like name, description, etc.
$product->setName($_POST['name']);
$product->setDescription($_POST['description']);
// ...
  1. Add the product to the cart with custom price:
// Start adding the product
$cart->addProduct($product, array(
    'qty' => $_POST['quantity'],
    'price' => floatval($_POST['custom_price']),
    // Optionally set other options like super attributes...
));
  1. Save and redirect to the cart page:
$cart->save();

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

header("Location: /checkout/cart");
exit();

Conclusion

Changing prices dynamically for products that don’t exist in your Magento backend is a complex task. However, by utilizing the core checkout and catalog APIs, you can achieve this with relative ease.

Remember, when working with large-scale projects like this, performance optimization is key. Avoid loading unnecessary models or running heavy queries on every page load. Use lazy loading where possible and leverage caching strategies to minimize server load.

For more advanced customization of your Magento store, consider checking out our comprehensive guides at Laravel Company.

Thank you for reading! If you have any further questions or need more specific code examples, feel free to leave a comment below. Happy coding!

Checkout Catalog Database API

Our network