Magento更新订单送货地址

时间:2013-07-26 13:23:40

标签: php 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 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

假设您要更新订单的送货地址,请查看/app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php中的@ addressSaveAction()

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

此外,我不确定{1}}在您提交表单后是否仍会包含有效的订单ID,因为您将加载新页面

答案 2 :(得分:2)

正确运行时,其他答案的地址加载过多。 getShippingAddress已经返回sales/order_address的实例。因此,程序可以简化为以下内容:

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

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

如果您使用多行街道地址,则只需要implodeStreetAddress方法调用。