Magento保存前更改订单地址

时间:2015-11-12 22:51:27

标签: php magento

我试图在magento中写一个观察员来改变送货地址,如果送货方式是扁平的。

我的问题是,我无法从会话中获取送货地址并保存我的价值。 下面我展示了我的代码

class Mydons_Eventdemo_Model_Observer {

    public function sales_order_save_before($observer)
    { 
      $order = $observer->getOrder();

        $shipping_address = $order->getShippingAddress();
        $shipping_method = $order->getShippingAddress()->getShippingMethod();


         if($shipping_method == 'flatrate_flatrate') {
          $shipping_address
          ->setStreet(('Street 45'))
          ->setCity('MyCity')
           ->setPostcode(('91-000'));
         }
//how to save shipping address record in this event ? 

    } 

}

有人可以告诉我如何以这种观察方法获取和更改送货地址(我需要检查送货方式)

感谢您的帮助 最诚挚的问候

更新: 感谢Chris Rogers,我已经更新了我的代码

UPDATE2: 下面的代码就像魅力一样;)

<?php

class Mydons_Eventdemo_Model_Observer {

    public function sales_order_save_before($observer)
    { 
      $order = $observer->getOrder();

        $shipping_address = $order->getShippingAddress();
        $shipping_method = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod();


         if($shipping_method == 'flatrate_flatrate') {
          $shipping_address
          ->setStreet('test street')
          ->setCity('Test City')
          ->setPostcode('91100');
         }
         $shipping_address->save();

    } 
}

1 个答案:

答案 0 :(得分:1)

使用观察者的 getOrder

$order = $observer->getOrder();
$shipping_address = $order->getShippingAddress();
$shipping_method = $shipping_address->getShippingMethod();
if($shipping_method == 'flatrate_flatrate') {
      $shipping_address
      ->setStreet('test street')
      ->setCity('Test City')
      ->setPostcode('91100');
}
$order->save();

您还需要保存订单,而不是地址。

如果您有任何问题,请告诉我。