Magento GetPrice Override Function

I have a custom SQL database in which we store different prices for each client for the product and for the quantity.

Now we want to set up the Magento online store and find out that we cannot import these prices into Magento because it does not match the data structure. (And Magento will probably melt due to the sheer amount of data that we will throw on it.)

So I think; can I leave the data where it is, configure the service on top of this data and just override the GetPrice () method in Magento to get the right price? (Thus, the correct price should appear on all possible pages showing prices. And more importantly, that price should also be used in baskets and orders.)

My question is: will it work like this? Just override one method, and do we have custom prices? Or do we need to change many other methods?

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

(Magento is completely new to me, and I spent half a day finding out tons of source code, but I'm still not sure if the whole application can answer my questions by looking at the code.)

+4
source share
3 answers

I have done this for several clients. It is quite doable.

First you should try to get the data on one server and get the results in real time. If the tables are well indexed (for example, by product_id), the loading time is quite acceptable.

, , .

, 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>

, getCustomPrice.

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;
    }
}

, Create magento custom module tutorial, .

+4

Mage_Catalog_Model_Product getFinalPrice(). - .

+1

1) catalog_product_get_final_price, getPrice. , , .

Option 2) Override Mage_Catalog_Model_Product::_afterLoad()and manually set any data variables, for example $this->setFinalPrice($price).

Pros: Most likely faster than events. Custom logic runs once when the product loads, and not every time getFinalPrice () is called.

Cons: Pay attention to other modules that may already have expanded the class.

+1
source

Source: https://habr.com/ru/post/1608845/


All Articles