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