Magento根据订单获取地址信息

20170406149146130740929.png

根据orderid获取address信息

代码实现:

1
2
3
4
5
6
7
8
9
10
11
$orderModel = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$address = $orderModel->getShippingAddress();
$street = $address->getStreet();
$address_data = array(
'name' => $address->getData('lastname').$address->getData('firstname'),//姓名
'phone' => $address->getData('telephone'),//手机号
'province' => $address->getData('region'),//省
'city' => $address->getData('city'),//市
'district' => $street[0],//区
'street' => $street[1],//街道
);

原理:

app/code/core/Mage/Sales/Model/Order.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public function getShippingAddress()
{
foreach ($this->getAddressesCollection() as $address) {
if ($address->getAddressType()=='shipping' && !$address->isDeleted()) {
return $address;
}
}
return false;
}
public function getAddressesCollection()
{
if (is_null($this->_addresses)) {
$this->_addresses = Mage::getResourceModel('sales/order_address_collection')
->setOrderFilter($this);
if ($this->getId()) {
foreach ($this->_addresses as $address) {
$address->setOrder($this);
}
}
}
return $this->_addresses;
}
app/code/core/Mage/Customer/Model/Address/Abstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* get address street
*
* @param int $line address line index
* @return string
*/
public function getStreet($line=0)
{
$street = parent::getData('street');
if (-1 === $line) {
return $street;
} else {
$arr = is_array($street) ? $street : explode("\n", $street);
if (0 === $line || $line === null) {
return $arr;
} elseif (isset($arr[$line-1])) {
return $arr[$line-1];
} else {
return '';
}
}
}