Set custom weight of simple product in Magento based on value of selected custom option

Set custom weight of simple product in Magento based on value of selected custom option

October 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Set custom weight of simple product in Magento based on value of selected custom option

Dynamically Setting Custom Product Weight in Magento

Magento is a powerful e-commerce platform that allows you to customize your online store in many ways. One common requirement for product configuration is the ability to dynamically set product weight based on selected options, such as material and size.

The Problem with Custom Product Weight Calculation

In your case, you have a Magento site that sells custom signs. You’ve set up custom options for customers to choose their preferred material and size. To calculate the sign’s weight accurately, you’re using JavaScript to perform the necessary calculations based on these options.

However, when it comes to passing this calculated weight back to Magento during the checkout process, you’re encountering a challenge. Despite successfully setting the weight via your custom module and observer class, the updated weight isn’t reflected in the quote or product object, leading to incorrect shipping rate calculations.

Understanding the Issue with Weight Setting

The key issue lies in the way Magento handles product attributes during the checkout process. When you attempt to set the weight directly on the product object using $product->setWeight($weight), Magento doesn’t recognize this change because it has already loaded the original data from the database for the quote and cart.

The Correct Approach: Manipulating the Quote Item

To resolve this issue, you need to work with the quote item instead of the product. When a new item is added to the cart (triggered by the checkout_product_add_after event), Magento creates a new quote item associated with the selected product.

Here’s how to correctly set the custom weight in your Observer.php file:

class XM_CustomWeight_Model_Observer {

  public function set_custom_weight($observer) {
    $event = $observer->getEvent();
    $item = $event->getQuoteItem();

    // Extract the calculated weight from your JavaScript logic or session
    $weight = /* Your calculated weight value here */;

    // Set the custom option on the item (if not already set)
    if (!$item->getOptionByCode('custom_weight')) {
      $option = new Varien_Object();
      $option->setProductId($item->getProduct()->getId());
      $option->setOptionId(/* Your custom weight option ID */);
      $option->setValue($weight);

      // Add the new option to the item
      $item->addOption($option);
    } else {
      // Update the existing option value
      $option = $item->getOptionByCode('custom_weight');
      $option->setValue($weight);
    }

    // Save the quote item (this triggers a recalculation of totals)
    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $quote->save();
  }
}

Important Considerations

  1. Custom Option ID: Make sure to replace /* Your custom weight option ID */ with the actual ID of the “Weight” custom option you’ve created.

  2. JavaScript Integration: The $weight variable should contain the value calculated by your JavaScript logic and stored in a session or cookie before the item is added to the cart.

  3. Quote Item Recalculation: The quote->save() method at the end of the function forces Magento to recalculate the subtotals, tax amounts, and shipping rates based on the new weight value.

Conclusion

Dynamically setting product weights in Magento requires careful handling of quote items during the checkout process. By following the approach outlined above—setting custom options directly on the quote item and triggering a total recalculation—you can ensure that your calculated weights are properly applied to the cart, leading to accurate shipping cost calculations.

Remember, this solution assumes you’re already capturing the weight value from JavaScript and passing it back to Magento before the checkout_product_add_after event is triggered. If you haven’t set up this part of the logic yet, ensure that you store the calculated weight in a session or cookie for later retrieval.

For more advanced customizations and performance optimizations in Magento, I recommend exploring the resources available on Laravel Company. They offer comprehensive guides and tutorials to help you make the most of this powerful e-commerce platform.

Checkout Catalog Database Extensions

Our network