Skip to content

Commit

Permalink
MAGE-838 Add reversion for customer group enablement
Browse files Browse the repository at this point in the history
  • Loading branch information
cammonro committed Jun 24, 2024
1 parent e42ed35 commit 114e078
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
10 changes: 10 additions & 0 deletions Helper/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,16 @@ public function isCustomerGroupsEnabled($storeId = null)
return $this->configInterface->isSetFlag(self::CUSTOMER_GROUPS_ENABLE, ScopeInterface::SCOPE_STORE, $storeId);
}

public function setCustomerGroupsEnabled(bool $val, ?string $scope = null, ?int $scopeId = null): void
{
$this->configWriter->save(
self::CUSTOMER_GROUPS_ENABLE,
$val ? '1' : '0',
$scope,
$scopeId
);
}

/**
* @param $storeId
* @return bool
Expand Down
3 changes: 3 additions & 0 deletions Model/Backend/EnableCustomerGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function __construct(
*/
public function afterSave(): Value
{
$this->replicaState->setAppliedScope($this->getScope(), $this->getScopeId());
$this->replicaState->setCustomerGroupsEnabled((bool) $this->getValue());

$storeIds = $this->configChecker->getAffectedStoreIds(
$this->getPath(),
$this->getScope(),
Expand Down
4 changes: 1 addition & 3 deletions Model/Backend/Sorts.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public function __construct(
*/
public function afterSave(): \Magento\Framework\App\Config\Value
{
// Save context of the admin operation for possible state reversion
$this->replicaState->setParentScope($this->getScope());
$this->replicaState->setParentScopeId($this->getScopeId());
$this->replicaState->setAppliedScope($this->getScope(), $this->getScopeId());

$storeIds = $this->configChecker->getAffectedStoreIds(
$this->getPath(),
Expand Down
8 changes: 8 additions & 0 deletions Model/Product/ReplicaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ protected function revertReplicaConfig(int $storeId): bool
return true;
}

if ($this->replicaState->wereCustomerGroupsEnabled()) {
$this->configHelper->setCustomerGroupsEnabled(
false,
$this->replicaState->getParentScope(),
$this->replicaState->getParentScopeId());
return true;
}

return false;
}

Expand Down
39 changes: 30 additions & 9 deletions Registry/ReplicaState.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,34 @@ class ReplicaState
private ?string $_parentScope = null;
private ?int $_parentScopeId = null;

private bool $_customerGroupsEnabled = false;

/**
* Save context of the original admin operation
* This is considered the "parent" scope.
* This is different from the affected store IDs for the applied/parent scope
* Retaining this is necessary for potential state reversion on error because while changes are persisted
* to Algolia indices for each store, the idea of default / website scope is a Magento only concept.
*
* @param string $scope
* @param int $scopeId
* @return void
*/
public function setAppliedScope(string $scope, int $scopeId): void {
$this->_parentScope = $scope;
$this->_parentScopeId = $scopeId;
}

public function getParentScope(): ?string
{
return $this->_parentScope;
}

public function setParentScope(string $scope) {
$this->_parentScope = $scope;
}

public function getParentScopeId(): ?int
{
return $this->_parentScopeId;
}

public function setParentScopeId(int $scopeId) {
$this->_parentScopeId = $scopeId;
}

public function getOriginalSortConfiguration(int $storeId): array
{
return $this->_scopeConfigurationOld[$storeId] ?? [];
Expand All @@ -55,7 +65,8 @@ public function setUpdatedSortConfiguration(array $updatedSortConfiguration, int
$this->_scopeConfigurationNew[$storeId] = $updatedSortConfiguration;
}

protected function hasConfigDataToCompare(int $storeId) {
protected function hasConfigDataToCompare(int $storeId): bool
{
return isset($this->_scopeConfigurationOld[$storeId])
&& isset($this->_scopeConfigurationNew[$storeId]);
}
Expand All @@ -79,4 +90,14 @@ public function setChangeState(int $state, int $storeId): void
$this->_scopeState[$storeId] = $state;
}

public function wereCustomerGroupsEnabled(): bool
{
return $this->_customerGroupsEnabled;
}

public function setCustomerGroupsEnabled(bool $customerGroupsEnabled): void
{
$this->_customerGroupsEnabled = $customerGroupsEnabled;
}

}

0 comments on commit 114e078

Please sign in to comment.