Search code examples
phpmagento

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


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


Solution

  • 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);
    

    ... }