Overwrite price depending custom options on Magento

Overwrite price depending custom options on Magento

October 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Overwrite price depending custom options on Magento

Custom Price Calculation for Magento Products

In Magento, custom options allow you to offer varied pricing for your products based on customer selections. However, when it comes to integrating custom price values from external sources like CSV spreadsheets, the default Magento behavior can override your calculations.

Understanding the Problem

Currently, you’ve managed to alter prices via JavaScript in the frontend before adding them to the shopping cart. Unfortunately, once the product is added to the cart or proceeds to checkout, Magento reverts back to its own price calculation logic, bypassing the custom price values you’ve set.

The provided JavaScript code successfully calculates and updates the product price based on selected options and custom price data from the database. However, this custom price isn’t being persisted when the user adds the item to their cart or proceeds through checkout.

Solving the Persistence Issue

To ensure Magento uses your calculated prices instead of its default pricing logic, you need to intercept the price calculation process at a point where Magento hasn’t yet overridden your values. This typically involves modifying the core Mage_Catalog_Model_Product class that handles product price calculation for the cart and checkout processes.

Update the Product Model

Locate the Mage_Catalog_Model_Product class file in your Magento installation (usually found under app/code/core/Mage/Catalog/Model/Product.php). Open this file, and look for the getPrice() method. In this method, you can override the default price calculation logic to use your custom prices.

public function getPrice()
{
    // Retrieve the product's price
    $price = parent::getPrice();

    // Check if we have a custom price stored in a custom attribute or from an external source like your CSV spreadsheet
    if ($customPrice = Mage::getSingleton('your_module/some_model')->getCustomPrice($this->getId())) {
        $price = $customPrice;
    }

    return $price;
}

In the above example, replace 'your_module' and 'some_model' with the appropriate module name and model class that handles retrieving custom prices from your external data source.

Persisting Custom Prices in the Cart

To ensure that the customized product price is preserved when added to the cart, you need to update the Mage_Checkout_Model_Cart class as well. In this case, override the addProduct() method to apply your custom prices during the item addition process.

public function addProduct($productInfo, $requestItem = null)
{
    // Perform initial product addition logic
    $result = parent::addProduct($productInfo, $requestItem);

    // Retrieve the cart item and apply custom price if available
    $cartItemId = $result->getCartItem()->getId();
    $customPrice = Mage::getSingleton('your_module/some_model')->getCustomPrice($productInfo['product_id']);

    if ($customPrice !== null) {
        $this->_getQuote()->updateItemsByQuoteId($this->getQuote()->getId(), array(
                $cartItemId => array(
                    'row_total' => $customPrice,
                )
            ))->save();
    }

    return $result;
}

Conclusion

To make Magento use your custom, calculated prices based on custom options and external data, you need to override the default price calculation logic in the Mage_Catalog_Model_Product class. Additionally, update the addProduct() method in the Mage_Checkout_Model_Cart class to preserve these custom prices when adding items to the cart.

Remember that directly modifying core Magento files is not recommended for production environments due to compatibility issues with upgrades. Consider creating your own module and overriding these methods via plugin or preference configuration as a more maintainable solution.

For detailed instructions on creating custom modules in Magento, refer to the official documentation and explore various other resources available at https://laravelcompany.com.

Checkout Catalog Database Customers

Our network