Skip to content

Commit

Permalink
Merge pull request #3799 from craftcms/feature/pt-2325-5x-cant-set-en…
Browse files Browse the repository at this point in the history
…vironment-variables-in-store-settings

[5.x] Correctly save environment variables on store settings
  • Loading branch information
nfourtythree authored Dec 4, 2024
2 parents 0db1d2f + 48e3dcb commit 7a400e5
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixed a bug where draft purchasables would show up on the Inventory page.
- Fixed a PHP error that could occur when creating inventory transfers. ([#3696](https://github.com/craftcms/commerce/issues/3696))
- 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 store settings weren’t respecting environment variables. ([#3786](https://github.com/craftcms/commerce/issues/3786))

## 5.2.7 - 2024-11

Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
92 changes: 92 additions & 0 deletions src/migrations/m241204_091901_fix_store_environment_variables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace craft\commerce\migrations;

use craft\commerce\db\Table;
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(Table::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(Table::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;
}

// Parse the value from the PC
$envVarValue = App::parseBooleanEnv($storeSettingsForStore[$storeProperty]);
if ($envVarValue === null) {
continue;
}

$updateData[$storeProperty] = $storeSettingsForStore[$storeProperty];
}

if (empty($updateData)) {
continue;
}

$this->update(Table::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;
}
}

0 comments on commit 7a400e5

Please sign in to comment.