Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Magento GetPrice function

I have a custom SQL database in which we have stored different prices per customer per product and per quantity.

Now we want to setup a Magento web store and found out that we cannot really import those prices into Magento because it does not match in terms of data structure. (And Magento would probably melt down because of the huge amounts of data we whould throw at it.)

So I'm thinking; can I leave the data where it is, setup a service on top of that data, and just override some GetPrice() method within Magento to get the right price? (That right price should then of course show up in every possible page that shows prices. And, even more important, that price should be used in baskets and orders too.)

My question is A: Would it work like that? Just overriding one method and we have custom pricing? Or do we have to alter many more methods?

And B: Where can I find that code/method to override?

(Magento is quite new for me. And did spend half a day looking through tons of source code, but i'm not yet that confident with the whole application that i could anwers my own questions by looking through the code.)

like image 505
Tys Avatar asked Mar 15 '23 16:03

Tys


2 Answers

I've done this for a few clients. This is totally doable.

First you should try to have data on the same server and fetch results live. If the tables are well indexed (on product_id for instance) load time is quite acceptable.

I usually use the observer approach, as it is less intrusive than overriding a catalog model method.

So to do that, create a module and configure an observer in config.xml :

</global>
    <events>
        <catalog_product_load_after>
            <observers>
                <productloadhandle>
                    <type>singleton</type>
                    <class>Yourcompany_Customprices_Model_Observer</class>
                    <method>getCustomPrice</method>
                </productloadhandle>
            </observers>
        </catalog_product_load_after>
        <catalog_product_get_final_price>
            <observers>
                <getfinalpricehandle>
                    <type>singleton</type>
                    <class>Yourcompany_Customprices_Model_Observer</class>
                    <method>getCustomPrice</method>
                </getfinalpricehandle>
            </observers>
        </catalog_product_get_final_price>
    </events>
</global>

Then create the observer model that will implement the getCustomPrice method.

class Yourcompany_Customprices_Model_Observer extends Mage_Core_Model_Abstract
{

    public function getCustomPrice($observer)
    {
        // If not logged in just get outta here
        if (! Mage::getSingleton('customer/session')->isLoggedIn()) {
            return;
        }

        $event = $observer->getEvent();
        if (Mage::getSingleton('customer/session')->isLoggedIn()) {
            // THis is where you will want to have your price mechanism going on.
            if (Mage::getSingleton('customer/session')->getCustomer()->getId() == $someIdFromCustomModuleTable && otherConditions) {
                $product = $event->getProduct();
                if ($product && null != $product->getSku()) {
                    // This is where you can tweak product price, using setPrice or setFinal
                    $product->setFinalPrice($customerPrice);
                }
            }
        }
        return $this;
    }
}

I wont get into details of the rest of the module which is quite standard and that you can find in any Create magento custom module tutorial, but that should get you started.

like image 113
baoutch Avatar answered Mar 20 '23 12:03

baoutch


You should override Mage_Catalog_Model_Product getFinalPrice(). In the overridden method you need to call your web service for price depending on customer group and quantity.

like image 27
Marko Novakovic Avatar answered Mar 20 '23 12:03

Marko Novakovic