加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 覆盖Magento GetPrice函数

发布时间:2020-12-13 17:49:18 所属栏目:PHP教程 来源:网络整理
导读:我有一个自定义SQL数据库,我们在每个产品和每个数量的每个客户存储不同的价格. 现在我们想要设置一个Magento网上商店,发现我们无法将这些价格真正导入Magento,因为它在数据结构方面不匹配. (而Magento可能会因为我们想要投入大量数据而融化.) 所以我在想;我
我有一个自定义SQL数据库,我们在每个产品和每个数量的每个客户存储不同的价格.

现在我们想要设置一个Magento网上商店,发现我们无法将这些价格真正导入Magento,因为它在数据结构方面不匹配. (而Magento可能会因为我们想要投入大量数据而融化.)

所以我在想;我可以将数据保留在原来的位置,在该数据之上设置服务,并在Magento中覆盖一些GetPrice()方法以获得合适的价格吗? (当然,正确的价格应显示在显示价格的每个可能页面中.而且,更重要的是,该价格也应该用在篮子和订单中.)

我的问题是答:它会像那样工作吗?只是覆盖一种方法,我们有自定义定价?或者我们是否必须改变更多方法?

B:我在哪里可以找到要覆盖的代码/方法?

(Magento对我来说很新鲜.并且确实花了半天时间查看大量的源代码,但我对整个应用程序还没有那么自信,我可以通过查看代码来回答我自己的问题.)

解决方法

我为一些客户做了这个.这是完全可行的.

首先,您应该尝试在同一台服务器上获取数据并实时获取结果.
如果表格编入索引(例如在product_id上),则加载时间是完全可以接受的.

我通常使用观察者方法,因为它比重写目录模型方法更少侵入性.

为此,在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自定义模块教程中找到,但这应该可以帮助您入门.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读