Update Shipping Address Similar to Billing Address before/after placing an order using Observer?

感情迁移 提交于 2019-12-25 06:45:29

问题


Brief: I am using "Magestore giftvoucher" module in that by default Shipping Address not getting saved, when an order is placed (with gift card product only). As Gift-voucher is not a Physical product and hence not been shipped. It goes to recipient by Email. But somehow I want to make shipping address similar to Billing Address.

So What I have tried : under app/code/local/Magestore/Giftvoucher/Model/Observer.php

In Function :-> orderSaveAfter I wrote below code but with no luck it was not working.

        $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();

Please suggest what is wrong over here! Thanks


回答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);

... }



来源:https://stackoverflow.com/questions/20580520/update-shipping-address-similar-to-billing-address-before-after-placing-an-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!