Price based on product custom options (width and height)

Price based on product custom options (width and height)

October 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Price based on product custom options (width and height)

Calculating Custom Product Prices Based on User Input in Magento

As an experienced e-commerce developer, I understand the challenge of pricing products based on user-defined dimensions or custom options. In your case, you’re selling rugs with customer-selectable width and height, and you need to calculate the price accordingly.

Understanding the Current Approach

You’ve already taken a commendable step by overriding the Mage_Catalog_Model_Product_Type_Price::getPrice() method in your custom product type. However, you’re facing two key issues:

  1. You can’t retrieve the width and height values input by the customer.
  2. The width and height attributes are not set for the product.

Retrieving Custom Option Values

The first step is to fetch the values of the ‘width’ and ‘height’ custom options selected by the customer during the cart addition process. To achieve this, you can leverage the getQuoteItemOptionByCode() method provided by the quote item model (Mage_Sales_Model_Quote_Item).

Here’s a function that retrieves the value for a specific custom option:

public function getOptionValue($quoteItem, $optionId)
{
    $customOptions = $quoteItem->getProduct()->getTypeInstance(true)->getOrderOptions($quoteItem->getProduct());
    foreach ($customOptions as $option) {
        if ($option['option_id'] == $optionId) {
            return $option['value'];
        }
    }
    return null;
}

You can use this function to get the values of the ‘width’ and ‘height’ options in your getPrice() method:

public function getPrice(Mage_Catalog_Model_Product $product)
{
    // Get the quote item for the current product
    $quoteItem = Mage::helper('checkout/session')->getQuote()->getItemByProduct($product);

    // Get the width and height values selected by the customer
    $width  = $this->getOptionValue($quoteItem, $product->getCustomOption('width')->getId());
    $height = $this->getOptionValue($quoteItem, $product->getCustomOption('height')->getId());

    if ($width && $height) {
        // Calculate the price based on the square meter price and custom dimensions
        return $width * $height * $product->getAttribute('square_meters_price');
    }

    return parent::getPrice($product);
}

Ensuring Attribute Values are Set

To prevent null values for ‘width’ and ‘height’, ensure that these attributes are set in your product. You can do this by creating custom attribute setup files or using the Magento admin interface to configure them.

For more detailed information on working with custom attributes, you might find our guide at Laravel Company’s Magento Custom Attribute Documentation helpful.

Conclusion

Calculating prices based on user-provided dimensions in Magento involves a combination of fetching custom option values from the quote item and ensuring that the necessary attributes are set for each product. By leveraging the getQuoteItemOptionByCode() method and creating helper functions to retrieve custom option values, you can achieve the desired functionality.

Please let me know if you have any further questions or need additional assistance with your Magento development projects. I’m always here to help!

Checkout Catalog Database Orders

Our network