且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Magento的购物车和/或结帐中获取运费

更新时间:2023-11-30 11:32:10

首先,在您看到某个实际瓶颈之前,不要过多地担心性能.对众多缓存系统抱有信心.讽刺的是,Magento已经有点像SQL了,所以如果您对商店进行了很好的调整,那么一些额外的查询也不会受到影响.

First, try not to worry too much about performance until you see an actual bottleneck somewhere. Have faith in the multitude of caching systems. Put more cynically, Magento's already a bit of a SQL beast, so if you have a store tuned well a few extra queries won't hurt.

第二,数据库命中甚至可能不是问题. shipping/rate_request模型似乎没有数据库支持.如果您两次查看它在核心代码中的使用情况

Second, the database hit might not even be a problem. The shipping/rate_request Model doesn't appear to be backed by a database. If you look at the two times it's used in the core code

Mage_Shipping_Model_Shipping::collectRatesByAddress
Mage_Sales_Model_Quote_Address::requestShippingRates

您可以看到正在实例化shipping/rate_request模型,然后从已经加载的字段中进行填充.此外,Mage_Shipping_Model_Carrier_Tablerate::collectRates中使用的所有模型都不会从数据库中加载任何内容,它们只是进行计算.

you can see the shipping/rate_request model is being instantiated, and then populated from already loaded fields. Additionally, all the models used in Mage_Shipping_Model_Carrier_Tablerate::collectRates don't load anything from a database, they just do calculations.

令人钦佩的是,您希望在初次尝试时就构建出性能尽可能高的东西,但是现代OO系统中存在太多复杂的交互作用,无法神奇地知道执行操作的最高性能方式.做必要的事情以获取所需的信息,并在维护版本(或者如果您没有足够的精力进行维护版本)进行性能调整(如果需要)时,当组织中有能力的人对某个地方的速度感到不满时)

It's admirable that you want to build something that's as performant as possible in the first go around, but there's too many complex interactions in a modern OO system to magically know the most performant way to do something. Do what you need to to get the information you need, and handle performance tuning (if needed) during a maintenance release (or if you're not lucky enough to have maintenance release, when someone with power in your organization grumbles about the speed somewhere)

第三,当系统不提供您需要的访问权限时,这就是类重写系统的作用.像

Third, when the system doesn't provide access to something yo need, that's what the class override system is for. Something like

class Package_Module_Model_Carriertablerate extends
Mage_Shipping_Model_Carrier_Tablerate
{
    public function getRate(Mage_Shipping_Model_Rate_Request $request)
    {
        $rate = parent::getRate($request);  
        Mage::register('package_module_carriertablerates', $rate);
        return $rate;
    }

}

...
//later, retrieve the rate
$rates = Mage::registry('package_module_carriertablerates');

您基本上在调用与以前相同的代码,但是将结果保存在某个地方以供以后访问.几乎可以像重写一样安全.

You're basically calling the same code as before, but stowing the results away somewhere for later access. About as safe as an override can get.