Magento php add to cart with Custom price

Magento php add to cart with Custom price

September 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento php add to cart with Custom price

Title: Solving Magento Custom Price Issue for Add-to-Cart Functionality

Introduction

In this post, we’ll address a common challenge faced by Magento developers - dynamically setting the price of a product when adding it to the cart from an AJAX request. We’ll explore a practical solution that allows you to calculate and apply custom prices based on frontend calculations.

Understanding the Problem

The core issue lies in the fact that Magento’s standard add-to-cart functionality doesn’t support dynamic, frontend-calculated prices by default. When using an AJAX controller to add products programmatically, you can’t directly set the price attribute of a product.

Martin has already implemented the basic structure for adding a product to the cart via an AJAX controller but struggles with setting the custom price based on his jQuery calculations in the frontend.

Proposed Solution: Custom Price Observer

The key to solving this problem is to create a custom event observer that listens for the ‘checkout_cart_product_add_after’ event. This event occurs after a product has been added to the cart, allowing us to modify its price attribute before it’s saved to the session.

Here’s a step-by-step guide to implementing this solution:

1. Create a Custom Module

First, create a new module in your Magento installation. For simplicity, let’s call it ‘YourCompany_CustomPrice’.

2. Declare Events and Observers in etc/config.xml

In the config.xml file of your module (located at app/code/local/YourCompany/CustomPrice/etc/config.xml), declare the event observer as follows:

<events>
    <checkout_cart_product_add_after>
        <observers>
            <customprice_observer>
                <type>singleton</type>
                <class>YourCompany_CustomPrice/Observer</class>
                <method>applyCustomPrice</method>
            </customprice_observer>
        </observers>
    </checkout_cart_product_add_after>
</events>

3. Create the Observer Class

Create a new file app/code/local/YourCompany/CustomPrice/Observer.php and define your observer class:

class YourCompany_CustomPrice_Model_Observer
{
    public function applyCustomPrice($observer)
    {
        // Get the added product from the event data
        $event = $observer->getEvent();
        $item = $event->getQuoteItem();

        // Get the custom price calculated in your jQuery script (Assuming you've sent it via an additional POST parameter)
        $customPrice = Mage::app()->getRequest()->getPost('your_custom_price_field');

        if ($customPrice) {
            // Set the new price for the added product
            $item->setOriginalCustomPrice($customPrice);
            $item->setCustomPrice($customPrice);

            // Save the quote item to apply the changes
            $quote = $item->getQuote();
            $quote->save();

            Mage::log('Custom price applied successfully: ' . $customPrice, null, 'yourcompany_customprice.log');
        }
    }
}

4. Send Custom Price from jQuery Script

Ensure that your jQuery script sends the calculated custom price as a POST parameter to your AJAX controller:

$.ajax({
    url: '/path-to-your-ajax-controller-zapfsaeule/ajaxadd/',
    type: 'POST',
    dataType: 'json',
    data: {
        zapfsaeule_product: JSON.stringify(productData),
        your_custom_price_field: customPrice // Include the calculated price here
    },
    success: function (result) {
        if (result.success === true) {
            // Handle success if needed
        }
    }
});

5. Update Your AJAX Controller

In your existing AJAX controller, you can now access the sent custom price and use it in your add-to-cart logic.

Conclusion

By following this approach, you can successfully set dynamic prices for products added to the cart via an AJAX request in Magento. This solution leverages event observers to intercept the add-to-cart process and apply the calculated prices before they are saved to the session.

Remember to clear your cache after implementing these changes, especially the configuration cache that stores the observer setup.

For more advanced customizations and best practices in Magento development, consider exploring our Magento Development Services at Laravel Company. We specialize in delivering high-quality solutions tailored to your business needs.

Happy coding!

Checkout Catalog Database Extensions

Our network