1
|
|
|
<?php |
2
|
|
|
namespace Jh\DataImportMagento\Writer; |
3
|
|
|
|
4
|
|
|
use Ddeboer\DataImport\Writer\AbstractWriter; |
5
|
|
|
use Jh\DataImportMagento\Exception\MagentoSaveException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class MagentoCustomerWriter |
9
|
|
|
* @author Aydin Hassan <[email protected]> |
10
|
|
|
* @package Jh\DataImportMagento\Writer |
11
|
|
|
*/ |
12
|
|
|
class CustomerWriter extends AbstractWriter |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Mage_Customer_Model_Customer |
17
|
|
|
*/ |
18
|
|
|
protected $customerModel; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Mage_Customer_Model_Address |
22
|
|
|
*/ |
23
|
|
|
protected $addressModel; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $regions = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $regionLookUpErrors = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \Mage_Customer_Model_Customer $customerModel |
37
|
|
|
* @param \Mage_Customer_Model_Address $addressModel |
38
|
|
|
* @param \Mage_Directory_Model_Resource_Region_Collection $regions |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
\Mage_Customer_Model_Customer $customerModel, |
42
|
|
|
\Mage_Customer_Model_Address $addressModel = null, |
43
|
|
|
\Mage_Directory_Model_Resource_Region_Collection $regions = null |
44
|
|
|
) { |
45
|
|
|
$this->customerModel = $customerModel; |
46
|
|
|
$this->addressModel = $addressModel; |
47
|
|
|
|
48
|
|
|
//load countries and regions |
49
|
|
|
if ($this->addressModel && $regions) { |
50
|
|
|
$this->regions = $this->processRegions($regions); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $item |
57
|
|
|
* @return $this |
58
|
|
|
* @throws MagentoSaveException |
59
|
|
|
*/ |
60
|
|
|
public function writeItem(array $item) |
61
|
|
|
{ |
62
|
|
|
$customer = clone $this->customerModel; |
63
|
|
|
|
64
|
|
|
//get address |
65
|
|
|
$addresses = []; |
66
|
|
|
if (isset($item['address'])) { |
67
|
|
|
$addresses = $item['address']; |
68
|
|
|
unset($item['address']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$customer->setData($item); |
72
|
|
|
|
73
|
|
|
//if we are adding addresses - create |
74
|
|
|
//model for each and set it on the customer |
75
|
|
|
if ($this->addressModel) { |
76
|
|
|
foreach ($addresses as $addressData) { |
77
|
|
|
//lookup region info: |
78
|
|
|
$name = ''; |
79
|
|
|
if (isset($addressData['firstname']) && $addressData['lastname']) { |
80
|
|
|
$name = $addressData['firstname'] . " " . $addressData['lastname']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$regionId = false; |
84
|
|
|
if (isset($addressData['region']) && $addressData['country_id']) { |
85
|
|
|
$regionId = $this->lookUpRegion($addressData['region'], $addressData['country_id'], $name); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($regionId) { |
89
|
|
|
$addressData['region_id'] = $regionId; |
90
|
|
|
unset($addressData['region']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$address = clone $this->addressModel; |
94
|
|
|
|
95
|
|
|
$address->setData($addressData); |
96
|
|
|
$address->setIsDefaultShipping(true); |
97
|
|
|
$address->setIsDefaultBilling(true); |
98
|
|
|
$customer->addAddress($address); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
$customer->save(); |
104
|
|
|
} catch (\Mage_Core_Exception $e) { |
105
|
|
|
$message = $e->getMessage(); |
106
|
|
|
if (isset($item['email'])) { |
107
|
|
|
$message .= " : " . $item['email']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
throw new MagentoSaveException($message); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param \Mage_Directory_Model_Resource_Region_Collection $regions |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function processRegions(\Mage_Directory_Model_Resource_Region_Collection $regions) |
121
|
|
|
{ |
122
|
|
|
$sortedRegions = array(); |
123
|
|
|
foreach ($regions as $region) { |
124
|
|
|
$countryId = $region->getData('country_id'); |
125
|
|
|
if (!isset($sortedRegions[$countryId])) { |
126
|
|
|
$sortedRegions[$countryId] = array( |
127
|
|
|
strtolower($region->getData('name')) => $region->getId() |
128
|
|
|
); |
129
|
|
|
} else { |
130
|
|
|
$sortedRegions[$countryId][strtolower($region->getData('name'))] = $region->getId(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
return $sortedRegions; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $regionText |
139
|
|
|
* @param string $countryId |
140
|
|
|
* @param string $name |
141
|
|
|
* @return int|bool |
142
|
|
|
*/ |
143
|
|
|
public function lookUpRegion($regionText, $countryId, $name) |
144
|
|
|
{ |
145
|
|
|
//country requires pre-defined state |
146
|
|
|
if (isset($this->regions[$countryId])) { |
147
|
|
|
if (isset($this->regions[$countryId][strtolower($regionText)])) { |
148
|
|
|
return $this->regions[$countryId][strtolower($regionText)]; |
149
|
|
|
} else { |
150
|
|
|
$this->regionLookUpErrors[] |
151
|
|
|
= "Customer '$name' has region '$regionText' from country '$countryId'. NOT FOUND"; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|