-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3786 correctly save environment variables on store settings
- Loading branch information
1 parent
4c12360
commit 6eb58c4
Showing
4 changed files
with
110 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/migrations/m241204_091901_fix_store_environment_variables.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace craft\commerce\migrations; | ||
|
||
use craft\db\Migration; | ||
use craft\db\Query; | ||
use craft\helpers\App; | ||
|
||
/** | ||
* m241204_091901_fix_store_environment_variables migration. | ||
*/ | ||
class m241204_091901_fix_store_environment_variables extends Migration | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeUp(): bool | ||
{ | ||
// Get all the stores current data | ||
$stores = (new Query()) | ||
->from('{{%commerce_stores}}') | ||
->all(); | ||
|
||
// Get the store settings for each store from the project config | ||
$storeSettings = \Craft::$app->getProjectConfig()->get('commerce.stores'); | ||
|
||
|
||
// Store properties to update | ||
$storeProperties = [ | ||
'autoSetNewCartAddresses', | ||
'autoSetCartShippingMethodOption', | ||
'autoSetPaymentSource', | ||
'allowEmptyCartOnCheckout', | ||
'allowCheckoutWithoutPayment', | ||
'allowPartialPaymentOnCheckout', | ||
'requireShippingAddressAtCheckout', | ||
'requireBillingAddressAtCheckout', | ||
'requireShippingMethodSelectionAtCheckout', | ||
'useBillingAddressForTax', | ||
'validateOrganizationTaxIdAsVatId', | ||
]; | ||
|
||
// Update stores env var DB columns | ||
foreach ($storeProperties as $storeProperty) { | ||
$this->alterColumn('{{%commerce_stores}}', $storeProperty, $this->string()->notNull()->defaultValue('false')); | ||
} | ||
|
||
// Loop through each store and update values in the DB to match the PC values | ||
foreach ($stores as $store) { | ||
$storeSettingsForStore = $storeSettings[$store['uid']] ?? null; | ||
|
||
// If there isn't data in the PC for this store, skip it | ||
if (!$storeSettingsForStore) { | ||
continue; | ||
} | ||
|
||
$updateData = []; | ||
foreach ($storeProperties as $storeProperty) { | ||
// If there isn't data in the PC for this store property, skip it | ||
if (!isset($storeSettingsForStore[$storeProperty])) { | ||
continue; | ||
} | ||
|
||
// If the value in PC is a bool and the same as the DB value, skip it | ||
if (in_array($storeSettingsForStore[$storeProperty], ['0', '1', 0, 1, false, true, 'false', 'true'], true) && $storeSettingsForStore[$storeProperty] == $store[$storeProperty]) { | ||
continue; | ||
} | ||
|
||
// If the value in PC is a string and is different from the DB value, skip it to avoid change in behavior | ||
$envVarValue = App::parseBooleanEnv($storeSettingsForStore[$storeProperty]); | ||
if ($envVarValue != $store[$storeProperty]) { | ||
continue; | ||
} | ||
|
||
// Else update the DB with the environment variable name | ||
$updateData[$storeProperty] = $storeSettingsForStore[$storeProperty]; | ||
} | ||
|
||
if (empty($updateData)) { | ||
continue; | ||
} | ||
|
||
$this->update('{{%commerce_stores}}', $updateData, ['id' => $store['id']]); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeDown(): bool | ||
{ | ||
echo "m241204_091901_fix_store_environment_variables cannot be reverted.\n"; | ||
return false; | ||
} | ||
} |