2013-07-26 135 views
1

在購買結束時,用戶可以更改您的送貨地址。我試圖更新這些信息,但它並不奏效。這是我的代碼:Magento更新訂單送貨地址

$customer = Mage::getModel('customer/session')->getCustomer(); 
$order = Mage::getSingleton('checkout/session')->getLastOrder(); 
$postData = Mage::app()->getRequest()->getPost(); 

$_new_address = array (
    'firstname' => $postData['nombre'], 
    'lastname' => $postData['apellidos'], 
    'street' => array ('0' => $postData['direccion']), 
    'city'  => $postData['localidad'], 
    'region_id' => $postData['provincia_id'], 
    'region' => '', 
    'postcode' => $postData['codigo_postal'], 
    'country_id'=> 'ES', 
    'telephone' => $postData['telefono'] 
); 

$customAddress = Mage::getModel('customer/address'); 
$customAddress->setData($_new_address) 
    ->setCustomerId($customer->getId()) 
    ->setIsDefaultBilling('1') 
    ->setIsDefaultShipping('1') 
    ->setSaveInAddressBook('1'); 

// Save address 
try { 
    $customAddress->save(); 
} catch (Exception $e) { 
    Mage::getSingleton('core/session')->addError($e->getMessage()); 
    header('Location: /'); 
    exit; 
} 

// Update the order 
try { 
    $order->setShippingAddress($customAddress)->save(); 
} catch (Exception $e) { 
    Mage::getSingleton('core/session')->addError($e->getMessage()); 
    header('Location: /'); 
    exit; 
} 

我可以更新訂單或不允許嗎?任何人都可以給我一個小費?

回答

3

我的問題是,訂單中的帳單地址和送貨地址與用戶不同具有作爲默認地址。

在結束時,代碼如下所示:

$order = Mage::getSingleton('checkout/session')->getLastOrder(); 

$postData = Mage::app()->getRequest()->getPost(); 

// Try to get shipping and billing address data. 
$orderShippingAddress = $order->getShippingAddress()->getId(); 
$orderShipping = Mage::getModel('sales/order_address')->load($orderShippingAddress); 

$orderBillingAddress = $order->getBillingAddress()->getId(); 
$orderBilling = Mage::getModel('sales/order_address')->load($orderBillingAddress); 

// Updating data. 
$orderShipping->addData($postData); 
$orderBilling->addData($postData); 

try { 
    $orderShipping->implodeStreetAddress()->save(); 
    $orderBilling->implodeStreetAddress()->save(); 
} catch (Exception $e) { 
    Mage::logException($e); 
    Mage::getSingleton('core/session')->addError($e->getMessage()); 
    header('Location: /after/success/envio'); 
    exit; 
} 

現在,它的工作原理。感謝您的幫助@ R.S

2

假設你要更新的送貨地址的訂單,看看@addressSaveAction()在/app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php

$order = Mage::getSingleton('checkout/session')->getLastOrder(); 

//Get shipping address Id 
$addressId = $order->getShippingAddress()->getId(); 

$address = Mage::getModel('sales/order_address')->load($addressId); 

$data  = $this->getRequest()->getPost(); 

$address->addData($data); 

$address->implodeStreetAddress()->save(); 

此外,我我不確定Mage::getSingleton('checkout/session')->getLastOrder()在您提交表單後是否仍然包含有效的訂單ID,因爲您將加載新頁面

+0

是的,getLastOrder()仍然包含有效的訂單ID。 – Javier

2

雖然正常運行,但其他答案都有過多的地址加載。 getShippingAddress已經返回sales/order_address的實例。因此,該過程可以簡化爲以下幾點:如果您使用多行的街道地址

$order = Mage::getSingleton('checkout/session')->getLastOrder(); 
$data = $this->getRequest()->getPost(); 

$order->getShippingAddress()->addData($data) 
    ->save(); 

而且implodeStreetAddress方法調用時才需要。

相關問題