0

I want to calculate price with formula: price = weight*height*width. How can I do that?

  • ...you mean to calculate by volume? – Anna Völkl Feb 6 '15 at 8:10
  • You can create a local extension where, price will be calculated for each product based on price = weight * height * width; – Rahul Kumar Das Feb 6 '15 at 8:15
  • Thank you for your respond, but I don't know how to get the value of weight, height, width. – tuanptit Feb 6 '15 at 8:22
  • No, I just want to calculate price by another attribute. Example: price = weight *height * width – tuanptit Feb 6 '15 at 8:28
  • You can create a formula for your product price using this extension - itoris.com/magento-product-price-formula.html – nico Aug 11 '15 at 1:57
4

There are several extensions that will do this for you. You could search on google for magento calculate price by volume for example.

If you want to do it yourself you'd have to create a module that observes both checkout_cart_product_add_after and checkout_cart_update_items_after and mutate the price based on those attributes.

Below some untested code so you'll have to tweak it but this is basically what should happen.

/app/code/local/[Namespace]/[Module]/etc/config.xml

<config>
    [...]
    <frontend>
        [...]
        <events>
            [...]
            <checkout_cart_product_add_after>
                <observers>
                    <[namespace]_[module]_checkout_cart_product_add_after>
                        <class>[Namespace]_[Module]_Model_Observer</class>
                        <method>checkoutCartProductAfter</method>
                    </[namespace]_[module]_checkout_cart_product_add_after>
                </observers>
            </checkout_cart_product_add_after>
            <checkout_cart_update_items_after>
                <observers>
                    <[namespace]_[module]_checkout_cart_update_items_after>
                        <class>[Namespace]_[Module]_Model_Observer</class>
                        <method>checkoutCartUpdateItemsAfter</method>
                    </[namespace]_[module]_checkout_cart_update_items_after>
                </observers>
            </checkout_cart_update_items_after>
            [...]
        </events>
        [...]
    </frontend>
    [...]
</config>

and /app/code/local/[Namespace]/[Module]/Model/Observer.php

class [Namespace]_[Module]_Model_Observer
{

    public function checkoutCartProductAfter(Varien_Event_Observer $observer)
    {
            $item = $observer->getQuoteItem();

            $this->_calculatePrice($item); // here we will set the price

            return $this;
    }

    public function checkoutCartUpdateItemsAfter(Varien_Event_Observer $observer)
    {
        $event = $observer->getEvent();
        $data = $event->getInfo();
        $quote = $event->getCart()->getQuote();

        foreach ($data as $itemId => $qty) // go through each updated item
        {
            $item = $quote->getItemById($itemId);
            $this->_calculatePrice($item); // here we will set the price
        }

        return $this;
    }

    protected function _calculatePrice(Mage_Sales_Model_Quote_Item $item)
    {
        $item = ($item->getParentItem()) ? $item->getParentItem() : $item; // make sure you have the top item

        $basePrice = $item->getPrice();
        $weight = $item->getProduct()->getWeight();
        $height = $item->getProduct()->getHeightAttribute();
        $width = $item->getProduct()->getWidthAttribute();

        $price = $basePrice * $weight * $height * $width;

        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);

        $item->getProduct()->setIsSuperMode(true);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.