drxt70655 2013-07-26 13:23
浏览 44
已采纳

Magento更新订单送货地址

At the end of the purchase, the user has the possibility to change your shipping address. Im trying to update this information, but it dosen't work. This is my code:

$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;
}

Can I update an order or is not allowed? Can anyone give me a tip?

  • 写回答

3条回答 默认 最新

  • doupian6118 2013-07-29 16:41
    关注

    My problem was that the Billing address and shipping address in the order, are different than having the user has as default addresses.

    At the end the code looks like this:

    $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;
    }
    

    Now, it works. Thanks for your help @R.S

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题