我有一个自定义SQL数据库,其中我们存储了每个客户每个产品和每个数量的不同价格。
现在我们想建立一个Magento网上商店,发现我们不能真正导入这些价格到Magento中,因为它在数据结构方面不匹配。(Magento可能会崩溃,因为我们会扔给它大量的数据。)
所以我在想;我是否可以保留数据,在该数据之上设置一个服务,并在Magento中重写一些GetPrice()方法来获得正确的价格?(当然,正确的价格应该出现在每一个可能显示价格的页面上。而且,更重要的是,价格也应该在篮子和订单中使用。
我的问题是A:它会这样工作吗?只重写一个方法,我们就有了自定义定价?还是我们必须改变更多的方法?和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;
}
}
我不会详细介绍该模块的其余部分,它是相当标准的,您可以在任何创建magento自定义模块教程中找到,但这应该可以让您开始。
您应该重写Mage_Catalog_Model_Product getFinalPrice()。在重写的方法中,您需要根据客户组和数量调用您的web服务来获取价格。
选项1)观察在多个产品型号和不同的getPrice方法中触发的事件catalog_product_get_final_price
。可能比逐一重写方法要容易得多。
选项2)覆盖Mage_Catalog_Model_Product::_afterLoad()
并手动设置任何数据变量,如$this->setFinalPrice($price)
。
优点:最有可能比事件更快。自定义逻辑在产品加载时运行一次,而不是在每次调用getFinalPrice()时运行。
缺点:注意其他可能已经扩展了这个类的模块