如何根据运输地址更改运输率
-
16-10-2019 - |
题
我为Magento商店添加了一个新的运输方法模块。该模块允许用户根据运输地址国家 /地区设置运输费率。
我添加了一个观察者,以节省客户的客户运输地址,该代码在下面。在 config.xml
我添加了新事件。
<events>
<checkout_controller_onepage_save_shipping_address_after>
<observers>
<zones>
<type>model</type>
<class>zones/observer</class>
<method>getShippingMethods</method>
</zones>
</observers>
</checkout_controller_onepage_save_shipping_address_after>
</events>
之后,我派遣了活动 OnepageController > SaveShipping
例如,
Mage::dispatchEvent('checkout_controller_onepage_save_shipping_address_after', array('page' => $model, 'request' => $this->getRequest()));
当我保存运输地址时,这会呼叫观察者。但是我不知道如何根据运输地址国家更改运输方法费用。
我使用了这个线程 http://www.opencart60s.com/magento/changing-or-set-shipping-price-price-on-fly-fly-fly-from-event-or-observer-in-magento.html-13407.html 更改运输成本。但是没有成功。
我需要按照国家来更改我的运输统一费率。
我已经创建了将存储该国和运输费率详细信息的模块。我可以使用该模块获得运输率。现在,我只需要使用自定义运输利率值来更新统一费率。请帮助我按照国家代码设定统一费率。谢谢..
解决方案
我在此处找到了此链接的解决方案: http://www.magentocommerce.com/boards/viewthread/316685/
在我的观察者中,我添加了下面的代码,以根据国家选择在运输地址中的选择来更改我的运输费率。
class Pnk_Zones_Model_Observer
{
public function getShippingMethods($observer)
{
$session = Mage::getSingleton('checkout/session');
$quote=Mage::getSingleton('checkout/session')->getQuote();
$quoteid=$quote->getId();
if($quoteid) {
try{
$address=$quote->getShippingAddress();
if($address->getAddressType()=='shipping'){
// echo '<pre>'; print_r($events->getQuoteAddress()->getData()); exit;
//$price=40;
$countryId = $address->getCountry();
//Code to get Shipping rate from my zone module //
$shippingRateCollection = Mage::getModel('zones/zones')->getCollection();
$shippingRateCollection->addFieldToSelect('*');
$shippingRateCollection->getSelect()->where("find_in_set('".$countryId."', `countries`)");
foreach ($shippingRateCollection as $shipping){
$price = $shipping->getShippingRate();
}
// echo "Price = ".$price;
// Find if our shipping has been included.
$rates = $address->collectShippingRates()
->getGroupedAllShippingRates();
foreach ($rates as $carrier) {
foreach ($carrier as $rate) {
// Check with your custom shipping method code
if($rate->getCode() == 'zones'){
$rate->setPrice($price);
$rate->save();
}
}
}
$this->collectTotals($quote,$price);
}
$quote->collectTotals();
} catch (Exception $e) {
Mage::logException($e);
$response['error'] = $e->getMessage();
}
}
}
public function collectTotals($quote,$price){
$quoteid=$quote->getId();
$shippingcode='freeshipping_freeshipping';
if($quoteid) {
try{
$quote->setSubtotal(0);
$quote->setBaseSubtotal(0);
$quote->setSubtotalWithDiscount(0);
$quote->setBaseSubtotalWithDiscount(0);
$quote->setGrandTotal(0);
$quote->setBaseGrandTotal(0);
$quote->getShippingAddress()->setShippingMethod($shippingcode)/* ->collectTotals() */->save();
$quote->save();
foreach ($quote->getAllAddresses() as $address) {
$address->setSubtotal(0);
$address->setBaseSubtotal(0);
$address->setGrandTotal(0);
$address->setBaseGrandTotal(0);
$address->collectTotals();
$quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
$quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());
$quote->setSubtotalWithDiscount(
(float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
);
$quote->setBaseSubtotalWithDiscount(
(float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
);
$quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
$quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());
$address->setShippingAmount($price);
$address->setBaseShippingAmount($price);
$address->save();
}
$response['message'] = 'Succcess';
} catch (Exception $e) {
Mage::logException($e);
$response['error'] = $e->getMessage();
}
}
}
}
其他提示
Thebod构建了一个为您完成此操作的模块,可能会有所帮助:https://github.com/thebod/thebod_shippingrates