1

I'm looking for a way to add a value to price of configurable product after all required options are selected. I need to match against full SKU of selected simple product so per option prices are not suitable

I've built an JSON with SKU - price pairs and now looking for an JS event to do the task cleanly and also preserve the new price during add to cart and checkout stages

Thanks in advance for any insights

Zifius
  • 1,650
  • 1
  • 16
  • 27

1 Answers1

4

You can use an observer class to listen to checkout_cart_product_add_after, and use a product's "Super Mode" to set custom prices against the quote item.

In your /app/code/local/{namespace}/{yourmodule}/etc/config.xml:

<config>
    ...
    <frontend>
        ...
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <unique_event_name>
                        <class>{{modulename}}/observer</class>
                        <method>modifyPrice</method>
                    </unique_event_name>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        ...
    </frontend>
    ...
</config>

And then create an Observer class at /app/code/local/{namespace}/{yourmodule}/Model/Observer.php

class <namespace>_<modulename>_Model_Observer
{
    public function modifyPrice(Varien_Event_Observer $obs)
    {
        // Get the quote item
        $item = $obs->getQuoteItem();
        // Ensure we have the parent item, if it has one
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        // Load the custom price
        $price = $this->_getPriceByItem($item);
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
    }

    protected function _getPriceByItem(Mage_Sales_Model_Quote_Item $item)
    {
        $price;

        //use $item and maybe your json object to determine the correct price

        return $price;
    }

}

This will handle the price changes from the backend. As for the javascript, I'm sorry but I'm not so sure!

Magento Guy
  • 2,493
  • 1
  • 16
  • 13
  • Accepted your answer as I managed to implement JS part :) One concern is left though - updating custom price when anonymous customer adds a product to cart and then logins to the store so the price must be recalculated, do you have any ideas on this? – Zifius Apr 06 '12 at 20:58
  • How did you do the JS? I'm interested to see what your solution was. As for the login issue, is the price being reset when the customer logs in? Maybe there's a better event to listen to, or maybe you can observe the login event and iterate each item. – Magento Guy Apr 12 '12 at 01:36
  • I've solved the pricing issue in `sales_quote_merge_after` event, this is where anonymous customer cart converted to logged in customer. As for JS I've built a complete JSON of option id to admin value pairs, collected full simple SKU and added value to final price (again from JSON) – Zifius Apr 13 '12 at 08:11