Magento根据订单获取地址信息

发布 : 2017-04-06 分类 : IT 浏览 :

代码实现:

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 '';
}
}
}
本文作者 : 小凡
原文链接 : https://16bh.github.io/2017/04/06/magento-get-address-data-by-order/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
留下足迹