diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d67c23cc..12e9e9f024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Fixed a bug where the price was not formatted correctly according to the locale in the payment model on the Order Edit screens. ([#3789](https://github.com/craftcms/commerce/issues/3789)) +- Fixed a bug where it wasn’t possible to set store settings to environment variables. ([#3786](https://github.com/craftcms/commerce/issues/3786)) ## 5.2.7 - 2024-11 diff --git a/src/Plugin.php b/src/Plugin.php index 8fef95aa8d..201e9b0bdb 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -257,7 +257,7 @@ public static function editions(): array /** * @inheritDoc */ - public string $schemaVersion = '5.2.7.0'; + public string $schemaVersion = '5.2.7.1'; /** * @inheritdoc diff --git a/src/migrations/Install.php b/src/migrations/Install.php index defc1bbf0c..756520a8ec 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -857,17 +857,17 @@ public function createTables(): void 'handle' => $this->string()->notNull(), 'primary' => $this->boolean()->notNull(), 'currency' => $this->string()->notNull()->defaultValue('USD'), - 'autoSetCartShippingMethodOption' => $this->boolean()->notNull()->defaultValue(false), - 'autoSetNewCartAddresses' => $this->boolean()->notNull()->defaultValue(false), - 'autoSetPaymentSource' => $this->boolean()->notNull()->defaultValue(false), - 'allowEmptyCartOnCheckout' => $this->boolean()->notNull()->defaultValue(false), - 'allowCheckoutWithoutPayment' => $this->boolean()->notNull()->defaultValue(false), - 'allowPartialPaymentOnCheckout' => $this->boolean()->notNull()->defaultValue(false), - 'requireShippingAddressAtCheckout' => $this->boolean()->notNull()->defaultValue(false), - 'requireBillingAddressAtCheckout' => $this->boolean()->notNull()->defaultValue(false), - 'requireShippingMethodSelectionAtCheckout' => $this->boolean()->notNull()->defaultValue(false), - 'useBillingAddressForTax' => $this->boolean()->notNull()->defaultValue(false), - 'validateOrganizationTaxIdAsVatId' => $this->boolean()->notNull()->defaultValue(false), + 'autoSetCartShippingMethodOption' => $this->string()->notNull()->defaultValue('false'), + 'autoSetNewCartAddresses' => $this->string()->notNull()->defaultValue('false'), + 'autoSetPaymentSource' => $this->string()->notNull()->defaultValue('false'), + 'allowEmptyCartOnCheckout' => $this->string()->notNull()->defaultValue('false'), + 'allowCheckoutWithoutPayment' => $this->string()->notNull()->defaultValue('false'), + 'allowPartialPaymentOnCheckout' => $this->string()->notNull()->defaultValue('false'), + 'requireShippingAddressAtCheckout' => $this->string()->notNull()->defaultValue('false'), + 'requireBillingAddressAtCheckout' => $this->string()->notNull()->defaultValue('false'), + 'requireShippingMethodSelectionAtCheckout' => $this->string()->notNull()->defaultValue('false'), + 'useBillingAddressForTax' => $this->string()->notNull()->defaultValue('false'), + 'validateOrganizationTaxIdAsVatId' => $this->string()->notNull()->defaultValue('false'), 'orderReferenceFormat' => $this->string(), 'freeOrderPaymentStrategy' => $this->string()->defaultValue('complete'), 'minimumTotalPriceStrategy' => $this->string()->defaultValue('default'), diff --git a/src/migrations/m241204_091901_fix_store_environment_variables.php b/src/migrations/m241204_091901_fix_store_environment_variables.php new file mode 100644 index 0000000000..4b11171509 --- /dev/null +++ b/src/migrations/m241204_091901_fix_store_environment_variables.php @@ -0,0 +1,97 @@ +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; + } +}