6
6
7
7
namespace Magento \Tax \Setup ;
8
8
9
+ use Magento \Directory \Model \ResourceModel \Region \CollectionFactory ;
9
10
use Magento \Framework \Setup \InstallDataInterface ;
10
11
use Magento \Framework \Setup \ModuleContextInterface ;
11
12
use Magento \Framework \Setup \ModuleDataSetupInterface ;
@@ -22,14 +23,32 @@ class InstallData implements InstallDataInterface
22
23
*/
23
24
private $ taxSetupFactory ;
24
25
26
+ /**
27
+ * Region collection factory.
28
+ *
29
+ * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
30
+ */
31
+ private $ regionCollectionFactory ;
32
+
33
+ /**
34
+ * Region collection.
35
+ *
36
+ * @var \Magento\Directory\Model\ResourceModel\Region\Collection
37
+ */
38
+ private $ regionCollection ;
39
+
25
40
/**
26
41
* Init
27
42
*
28
43
* @param TaxSetupFactory $taxSetupFactory
44
+ * @param CollectionFactory $collectionFactory
29
45
*/
30
- public function __construct (TaxSetupFactory $ taxSetupFactory )
31
- {
46
+ public function __construct (
47
+ TaxSetupFactory $ taxSetupFactory ,
48
+ CollectionFactory $ collectionFactory
49
+ ) {
32
50
$ this ->taxSetupFactory = $ taxSetupFactory ;
51
+ $ this ->regionCollectionFactory = $ collectionFactory ;
33
52
}
34
53
35
54
/**
@@ -101,22 +120,49 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface
101
120
[
102
121
'tax_calculation_rate_id ' => 1 ,
103
122
'tax_country_id ' => 'US ' ,
104
- 'tax_region_id ' => 12 ,
123
+ 'tax_region_id ' => $ this -> getRegionId ( ' CA ' ) ,
105
124
'tax_postcode ' => '* ' ,
106
125
'code ' => 'US-CA-*-Rate 1 ' ,
107
126
'rate ' => '8.2500 ' ,
108
127
],
109
128
[
110
129
'tax_calculation_rate_id ' => 2 ,
111
130
'tax_country_id ' => 'US ' ,
112
- 'tax_region_id ' => 43 ,
131
+ 'tax_region_id ' => $ this -> getRegionId ( ' NY ' ) ,
113
132
'tax_postcode ' => '* ' ,
114
133
'code ' => 'US-NY-*-Rate 1 ' ,
115
134
'rate ' => '8.3750 '
116
135
],
117
136
];
137
+
118
138
foreach ($ data as $ row ) {
119
139
$ setup ->getConnection ()->insertForce ($ setup ->getTable ('tax_calculation_rate ' ), $ row );
120
140
}
121
141
}
142
+
143
+ /**
144
+ * Return region id by code.
145
+ *
146
+ * @param string $regionCode
147
+ * @return string
148
+ */
149
+ private function getRegionId ($ regionCode )
150
+ {
151
+ if ($ this ->regionCollection === null ) {
152
+ /** @var \Magento\Directory\Model\ResourceModel\Region\Collection $regionCollection */
153
+ $ this ->regionCollection = $ this ->regionCollectionFactory ->create ();
154
+ $ this ->regionCollection ->addCountryFilter ('US ' )
155
+ ->addRegionCodeOrNameFilter (['CA ' , 'NY ' ]);
156
+ }
157
+
158
+ $ regionId = '' ;
159
+
160
+ /** @var \Magento\Directory\Model\Region $item */
161
+ $ item = $ this ->regionCollection ->getItemByColumnValue ('code ' , $ regionCode );
162
+ if ($ item ) {
163
+ $ regionId = $ item ->getId ();
164
+ }
165
+
166
+ return $ regionId ;
167
+ }
122
168
}
0 commit comments