Skip to content

Commit c35090c

Browse files
committed
MAGETWO-91599: Table Rates not working for Armed Forces states in Europe (AE)
- Use for region array
1 parent e8fda7c commit c35090c

File tree

5 files changed

+82
-35
lines changed

5 files changed

+82
-35
lines changed

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate/CSV/RowParser.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,25 @@ public function parse(
7171
}
7272

7373
$countryId = $this->getCountryId($rowData, $rowNumber, $columnResolver);
74-
$regionId = $this->getRegionId($rowData, $rowNumber, $columnResolver, $countryId);
74+
$regionIds = $this->getRegionIds($rowData, $rowNumber, $columnResolver, $countryId);
7575
$zipCode = $this->getZipCode($rowData, $columnResolver);
7676
$conditionValue = $this->getConditionValue($rowData, $rowNumber, $conditionFullName, $columnResolver);
7777
$price = $this->getPrice($rowData, $rowNumber, $columnResolver);
7878

79-
return [
80-
'website_id' => $websiteId,
81-
'dest_country_id' => $countryId,
82-
'dest_region_id' => $regionId,
83-
'dest_zip' => $zipCode,
84-
'condition_name' => $conditionShortName,
85-
'condition_value' => $conditionValue,
86-
'price' => $price,
87-
];
79+
$rates = [];
80+
foreach ($regionIds as $regionId) {
81+
$rates[] = [
82+
'website_id' => $websiteId,
83+
'dest_country_id' => $countryId,
84+
'dest_region_id' => $regionId,
85+
'dest_zip' => $zipCode,
86+
'condition_name' => $conditionShortName,
87+
'condition_value' => $conditionValue,
88+
'price' => $price,
89+
];
90+
}
91+
92+
return $rates;
8893
}
8994

9095
/**
@@ -120,17 +125,17 @@ private function getCountryId(array $rowData, $rowNumber, ColumnResolver $column
120125
* @param int $rowNumber
121126
* @param ColumnResolver $columnResolver
122127
* @param int $countryId
123-
* @return int|string
128+
* @return array
124129
* @throws ColumnNotFoundException
125130
* @throws RowException
126131
*/
127-
private function getRegionId(array $rowData, $rowNumber, ColumnResolver $columnResolver, $countryId)
132+
private function getRegionIds(array $rowData, $rowNumber, ColumnResolver $columnResolver, $countryId)
128133
{
129134
$regionCode = $columnResolver->getColumnValue(ColumnResolver::COLUMN_REGION, $rowData);
130135
if ($countryId !== '0' && $this->locationDirectory->hasRegionId($countryId, $regionCode)) {
131-
$regionId = $this->locationDirectory->getRegionId($countryId, $regionCode);
136+
$regionIds = $this->locationDirectory->getRegionIds($countryId, $regionCode);
132137
} elseif ($regionCode === '*' || $regionCode === '') {
133-
$regionId = 0;
138+
$regionIds = [0];
134139
} else {
135140
throw new RowException(
136141
__(
@@ -141,7 +146,7 @@ private function getRegionId(array $rowData, $rowNumber, ColumnResolver $columnR
141146
)
142147
);
143148
}
144-
return $regionId;
149+
return $regionIds;
145150
}
146151

147152
/**

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate/Import.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function getData(ReadInterface $file, $websiteId, $conditionShortName, $c
135135
if (empty($csvLine)) {
136136
continue;
137137
}
138-
$rowData = $this->rowParser->parse(
138+
$rowsData = $this->rowParser->parse(
139139
$csvLine,
140140
$rowNumber,
141141
$websiteId,
@@ -144,20 +144,25 @@ public function getData(ReadInterface $file, $websiteId, $conditionShortName, $c
144144
$columnResolver
145145
);
146146

147-
// protect from duplicate
148-
$hash = $this->dataHashGenerator->getHash($rowData);
149-
if (array_key_exists($hash, $this->uniqueHash)) {
150-
throw new RowException(
151-
__(
152-
'Duplicate Row #%1 (duplicates row #%2)',
153-
$rowNumber,
154-
$this->uniqueHash[$hash]
155-
)
156-
);
147+
foreach ($rowsData as $rowData) {
148+
// protect from duplicate
149+
$hash = $this->dataHashGenerator->getHash($rowData);
150+
if (array_key_exists($hash, $this->uniqueHash)) {
151+
throw new RowException(
152+
__(
153+
'Duplicate Row #%1 (duplicates row #%2)',
154+
$rowNumber,
155+
$this->uniqueHash[$hash]
156+
)
157+
);
158+
}
159+
$this->uniqueHash[$hash] = $rowNumber;
160+
161+
$items[] = $rowData;
162+
}
163+
if (count($rowsData) > 1) {
164+
$bunchSize += count($rowsData) - 1;
157165
}
158-
$this->uniqueHash[$hash] = $rowNumber;
159-
160-
$items[] = $rowData;
161166
if (count($items) === $bunchSize) {
162167
yield $items;
163168
$items = [];

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate/LocationDirectory.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class LocationDirectory
1313
*/
1414
protected $regions;
1515

16+
/**
17+
* @var array
18+
*/
19+
private $regionsByCode;
20+
1621
/**
1722
* @var array
1823
*/
@@ -115,16 +120,21 @@ public function hasRegionId($countryId, $regionCode)
115120
*/
116121
protected function loadRegions()
117122
{
118-
if ($this->regions !== null) {
123+
if ($this->regions !== null && $this->regionsByCode !== null) {
119124
return $this;
120125
}
121126

122127
$this->regions = [];
128+
$this->regionsByCode = [];
123129

124130
/** @var $collection \Magento\Directory\Model\ResourceModel\Region\Collection */
125131
$collection = $this->_regionCollectionFactory->create();
126132
foreach ($collection->getData() as $row) {
127133
$this->regions[$row['country_id']][$row['code']] = (int)$row['region_id'];
134+
if (empty($this->regionsByCode[$row['country_id']][$row['code']])) {
135+
$this->regionsByCode[$row['country_id']][$row['code']] = [];
136+
}
137+
$this->regionsByCode[$row['country_id']][$row['code']][] = (int)$row['region_id'];
128138
}
129139

130140
return $this;
@@ -134,10 +144,24 @@ protected function loadRegions()
134144
* @param int $countryId
135145
* @param string $regionCode
136146
* @return string
147+
* @deprecated
137148
*/
138149
public function getRegionId($countryId, $regionCode)
139150
{
140151
$this->loadRegions();
141152
return $this->regions[$countryId][$regionCode];
142153
}
154+
155+
/**
156+
* Return region ids for country and region
157+
*
158+
* @param $countryId
159+
* @param $regionCode
160+
* @return array
161+
*/
162+
public function getRegionIds($countryId, $regionCode)
163+
{
164+
$this->loadRegions();
165+
return $this->regionsByCode[$countryId][$regionCode];
166+
}
143167
}

app/code/Magento/OfflineShipping/Test/Unit/Model/ResourceModel/Carrier/Tablerate/CSV/RowParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class RowParserTest extends \PHPUnit\Framework\TestCase
3737
protected function setUp()
3838
{
3939
$this->locationDirectoryMock = $this->getMockBuilder(LocationDirectory::class)
40-
->setMethods(['hasCountryId', 'getCountryId', 'hasRegionId', 'getRegionId'])
40+
->setMethods(['hasCountryId', 'getCountryId', 'hasRegionId', 'getRegionIds'])
4141
->disableOriginalConstructor()
4242
->getMock();
4343
$this->columnResolverMock = $this->getMockBuilder(ColumnResolver::class)
@@ -92,7 +92,7 @@ public function testParse()
9292
$conditionShortName,
9393
$columnValueMap
9494
);
95-
$this->assertEquals($expectedResult, $result);
95+
$this->assertEquals([$expectedResult], $result);
9696
}
9797

9898
/**

app/code/Magento/OfflineShipping/Test/Unit/Model/ResourceModel/Carrier/Tablerate/ImportTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ protected function setUp()
7777
->getMock();
7878
$this->dataHashGeneratorMock = $this->getMockBuilder(DataHashGenerator::class)
7979
->getMock();
80-
$this->rowParserMock->expects($this->any())
81-
->method('parse')
82-
->willReturnArgument(0);
8380
$this->dataHashGeneratorMock->expects($this->any())
8481
->method('getHash')
8582
->willReturnCallback(
@@ -124,6 +121,15 @@ public function testGetData()
124121
['a4', 'b4', 'c4', 'd4', 'e4'],
125122
['a5', 'b5', 'c5', 'd5', 'e5'],
126123
];
124+
$this->rowParserMock->expects($this->any())
125+
->method('parse')
126+
->willReturn(
127+
[['a1', 'b1', 'c1', 'd1', 'e1']],
128+
[['a2', 'b2', 'c2', 'd2', 'e2']],
129+
[['a3', 'b3', 'c3', 'd3', 'e3']],
130+
[['a4', 'b4', 'c4', 'd4', 'e4']],
131+
[['a5', 'b5', 'c5', 'd5', 'e5']]
132+
);
127133
$file = $this->createFileMock($lines);
128134
$expectedResult = [
129135
[
@@ -167,6 +173,13 @@ public function testGetDataWithDuplicatedLine()
167173
[],
168174
['a2', 'b2', 'c2', 'd2', 'e2'],
169175
];
176+
$this->rowParserMock->expects($this->any())
177+
->method('parse')
178+
->willReturn(
179+
[['a1', 'b1', 'c1', 'd1', 'e1']],
180+
[['a1', 'b1', 'c1', 'd1', 'e1']],
181+
[['a2', 'b2', 'c2', 'd2', 'e2']]
182+
);
170183
$file = $this->createFileMock($lines);
171184
$expectedResult = [
172185
[

0 commit comments

Comments
 (0)