更新送货地址类似于使用Observer下订单之前/之后的账单地址?

时间:2013-12-14 06:44:54

标签: magento shipping

简介:我正在使用“Magestore giftvoucher”模块,默认发货地址未保存,订货时(仅限礼品卡产品)。礼品券不是实体产品,因此未发货。它通过电子邮件发送给收件人。但不知何故,我想发货地址类似于账单地址。

所以我尝试过: 在app / code / local / Magestore / Giftvoucher / Model / Observer.php

在功能中: - > orderSaveAfter 我写了下面的代码,但没有运气它没有用。

        $order = $observer->getEvent()->getOrder(); 
        $billing_address = $order->getBillingAddress();
        $shippingAddress = Mage::getModel('sales/order_address')
        ->setCustomerId($billing_address->getCustomerId())
        ->setCustomerAddressId($billing_address->getCustomerAddressId())
        ->setFirstname($billing_address->getFirstname())
        ->setMiddlename($billing_address->getMiddlename())
        ->setLastname($billing_address->getLastname())
        ->setSuffix($billing_address->getSuffix())
        ->setCompany($billing_address->getCompany())
        ->setStreet($billing_address->getStreet())
        ->setCity($billing_address->getCity())
        ->setCountry_id($billing_address->getCountryId())
        ->setRegion($billing_address->getRegion())
        ->setRegion_id($billing_address->getRegionId())
        ->setPostcode($billing_address->getPostcode())
        ->setTelephone($billing_address->getTelephone())
        ->setFax($billing_address->getFax());

        $order = Mage::getModel('sales/order')->load($order->getId());
        $order->setShippingAddress($shippingAddress);
        $order->save();

请在这里建议有什么问题! 感谢

1 个答案:

答案 0 :(得分:1)

you need to add code in sales_order_place_before event
public function orderPlaceBefore($observer) {

    $order = $observer->getEvent()->getOrder();
    $billing_address = $order->getBillingAddress();
    $shippingAddress = Mage::getModel('sales/order_address')
        ->setCustomerId($billing_address->getCustomerId())
        ->setCustomerAddressId($billing_address->getCustomerAddressId())
        ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
        ->setFirstname($billing_address->getFirstname())
        ->setMiddlename($billing_address->getMiddlename())
        ->setLastname($billing_address->getLastname())
        ->setSuffix($billing_address->getSuffix())
        ->setCompany($billing_address->getCompany())
        ->setStreet($billing_address->getStreet())
        ->setCity($billing_address->getCity())
        ->setCountry_id($billing_address->getCountryId())
        ->setRegion($billing_address->getRegion())
        ->setRegion_id($billing_address->getRegionId())
        ->setPostcode($billing_address->getPostcode())
        ->setTelephone($billing_address->getTelephone())
        ->setFax($billing_address->getFax());

    $order->setShippingAddress($shippingAddress);

... }