我有套餐运费方式,运费价格设置为5英镑,当小计超过35英镑(导入csv文件)时,也设置免运费。此外,商店还有额外的货币(EUR)。我必须将观察员的运费价格改为6欧元(即4.xx英镑)和免费运送小计到40欧元(将以磅计算)。这种行为背后的逻辑是创建的,但问题是我无法更改运费。
注意:我在SE和Magento论坛上发现了许多类似的问题,但没有一个解决方案适合我。
使用的事件是sales_quote_collect_totals_before
。
以下是观察者的方法:
public function salesQuoteCollectTotalsBefore(Varien_Event_Observer $observer)
{
try {
$quote = $observer->getQuote();
$store = Mage::app()->getStore($quote->getStoreId());
$address = $quote->getShippingAddress();
if($address->getCountry() == 'IE') {
$currencyRates = Mage::getModel('directory/currency')
->getCurrencyRates(Mage::app()->getBaseCurrencyCode(), array_values(Mage::getModel('directory/currency')
->getConfigAllowCurrencies()));
$shippingCostInEur = 6;
$freeShippingSubtotalInEur = 40;
$isFreeShipping = false;
$isDifferentCost = false;
$newCost = 0;
if(Mage::app()->getStore()->getCurrentCurrencyCode() == 'EUR') {
$subtotal = $quote->getSubtotal();
if($subtotal >= $freeShippingSubtotalInEur) {
$isFreeShipping = true;
} else {
$isDifferentCost = true;
$newCost = $shippingCostInEur;
}
} else {
$baseSubtotal = $quote->getBaseSubtotal();
$subtotalInEur = $baseSubtotal*$currencyRates['EUR'];
if($subtotalInEur >= $freeShippingSubtotalInEur) {
$isFreeShipping = true;
} else {
$isDifferentCost = true;
$newCost = $shippingCostInEur/$currencyRates['EUR'];
}
}
if($isFreeShipping) {
//$address->setFreeShipping(true);
$address->setShippingMethod('freeshipping_freeshipping');
//$carrierCode = 'tablerate';
//$store->setConfig("carriers/{$carrierCode}/handling_fee", 0);
}
if($isDifferentCost) {
$address->setShippingAmount($newCost);
//$store->setConfig("carriers/{$carrierCode}/handling_fee", $newCost);
}
if($isDifferentCost || $isFreeShipping) {
$quote->setTotalsCollectedFlag(true)
->collectTotals();
//->save();
}
}
} catch(Exception $e) {
Mage::log('BOOM: '.$e->getMessage(), null, 'test.log', true);
}
return $this;
}
有什么想法吗?我也尝试了this免费送货,它有效,但我不确定重写核心方法是否是个好主意。即使我使用它免费送货,当然还存在另一个运费的问题。
将Magento 1.9.0.1与Onepage结帐使用。
编辑:通过以下示例,一切似乎都有效,但我不确定这是否是一个好方法。据我所知,我只是更改了耦合XML中保存的配置值。不确定它是否足够节省。另外,我只更改handling_fee
而不是价格。这是:
public function salesQuoteCollectTotalsBefore(Varien_Event_Observer $observer)
{
$quote = $observer->getQuote();
$address = $quote->getShippingAddress();
$store = Mage::app()->getStore($quote->getStoreId());
if($address->getCountry() == 'IE') {
$currencyRates = Mage::getModel('directory/currency')
->getCurrencyRates(Mage::app()->getBaseCurrencyCode(), array_values(Mage::getModel('directory/currency')
->getConfigAllowCurrencies()));
$shippingCostInEur = (float)$store->getConfig('carriers/eservice_shippingrates/fixed_shipping_price');
$freeShippingSubtotalInEur = (float)$store->getConfig('carriers/eservice_shippingrates/free_shipping_min_subtotal');
$newCost = $shippingCostInEur/$currencyRates['EUR'];
if(Mage::app()->getStore()->getCurrentCurrencyCode() == 'EUR') {
$subtotalInEur = $quote->getSubtotal();
} else {
$baseSubtotal = $quote->getBaseSubtotal();
$subtotalInEur = $baseSubtotal*$currencyRates['EUR'];
}
if($subtotalInEur >= $freeShippingSubtotalInEur) {
$store->setConfig('carriers/tablerate/name', 'Free');
$newCost = 0;
} else {
$store->setConfig('carriers/tablerate/name', $store->getConfig('carriers/eservice_shippingrates/method_name'));
}
$store->setConfig('carriers/tablerate/title', $store->getConfig('carriers/eservice_shippingrates/title'));
$store->setConfig("carriers/tablerate/handling_fee", $newCost);
}
return $this;
}