Skip to content

Commit

Permalink
Fixed #1512
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Jun 17, 2020
1 parent cd17e96 commit fb4cba2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
### Changed
- Discounts now take into account discount adjustments created by custom discount adjusters. ([#1506](https://github.com/craftcms/commerce/issues/1506))

### Fixed
- Fixed a bug where countries and states would not return in their sorted order. ([#1512](https://github.com/craftcms/commerce/issues/1512))

## 3.1.8 - 2020-06-11

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static function t($message, $params = [], $language = null)
* @inheritDoc
*/

public $schemaVersion = '3.1.12';
public $schemaVersion = '3.1.13';

/**
* @inheritdoc
Expand Down
16 changes: 13 additions & 3 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,15 @@ private function _defaultCountries()
['ZW', 'Zimbabwe'],
];

$this->batchInsert(Table::COUNTRIES, ['iso', 'name'], $countries);
$orderNumber = 1;
foreach ($countries as $key => $country)
{
$country[] = $orderNumber;
$countries[$key] = $country;
$orderNumber++;
}

$this->batchInsert(Table::COUNTRIES, ['iso', 'name', 'sortOrder'], $countries);
}

/**
Expand Down Expand Up @@ -1525,12 +1533,14 @@ private function _defaultStates()

$rows = [];
foreach ($states as $iso => $list) {
$sortNumber = 1;
foreach ($list as $abbr => $name) {
$rows[] = [$code2id[$iso], $abbr, $name];
$rows[] = [$code2id[$iso], $abbr, $name, $sortNumber];
$sortNumber++;
}
}

$this->batchInsert(State::tableName(), ['countryId', 'abbreviation', 'name'], $rows);
$this->batchInsert(State::tableName(), ['countryId', 'abbreviation', 'name', 'sortOrder'], $rows);
}

/**
Expand Down
55 changes: 55 additions & 0 deletions src/migrations/m200617_172414_fix_country_state_sort_orders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace craft\commerce\migrations;

use craft\db\Migration;
use craft\db\Query;

/**
* m200617_172414_fix_country_state_sort_orders migration.
*/
class m200617_172414_fix_country_state_sort_orders extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$countries = (new Query())
->select(['*'])
->from('{{%commerce_countries}}')
->limit(null)
->orderBy(['name' => SORT_ASC])
->where(['sortOrder' => null])
->all();

$sortNumber = 1000; //start at 1000 so it doesn't affect if any countries had a sort previous
foreach ($countries as $country) {
$this->update('{{%commerce_countries}}', ['sortOrder' => $sortNumber], ['id' => $country['id']]);
$sortNumber++;
}

$states = (new Query())
->select(['*'])
->from('{{%commerce_states}}')
->limit(null)
->orderBy(['name' => SORT_ASC])
->where(['sortOrder' => null])
->all();

$sortNumber = 1000; //start at 1000 so it doesn't affect if any states had a sort previous
foreach ($states as $state) {
$this->update('{{%commerce_states}}', ['sortOrder' => $sortNumber], ['id' => $state['id']]);
$sortNumber++;
}
}

/**
* @inheritdoc
*/
public function safeDown()
{
echo "m200617_172414_fix_country_state_sort_orders cannot be reverted.\n";
return false;
}
}

0 comments on commit fb4cba2

Please sign in to comment.