From 8f0ea9e3a6dd5314737a619a0eb36d27456cb623 Mon Sep 17 00:00:00 2001 From: julien Date: Mon, 26 Jul 2021 16:16:52 +0200 Subject: [PATCH 1/2] fix custom group configuration --- EventListener/SystemConfigListener.php | 56 ++++++++++++++++++++++ Processors/AutoCustomerGroupAssignment.php | 10 ++-- Resources/config/services.yml | 7 +++ 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 EventListener/SystemConfigListener.php diff --git a/EventListener/SystemConfigListener.php b/EventListener/SystemConfigListener.php new file mode 100644 index 0000000..87e5fca --- /dev/null +++ b/EventListener/SystemConfigListener.php @@ -0,0 +1,56 @@ +registry = $registry; + } + + public function onFormPreSetData(ConfigSettingsUpdateEvent $event): void + { + $settingsKey = implode(ConfigManager::SECTION_VIEW_SEPARATOR, [AutoCustomerGroupExtension::ALIAS, Configuration::ASSIGNMENT_CUSTOMER_GROUP]); + $settings = $event->getSettings(); + if (is_array($settings) + && !empty($settings[$settingsKey]['value']) + ) { + $settings[$settingsKey]['value'] = $this->registry + ->getManagerForClass(CustomerGroup::class) + ->find(CustomerGroup::class, $settings[$settingsKey]['value']); + $event->setSettings($settings); + } + } + + public function onSettingsSaveBefore(ConfigSettingsUpdateEvent $event): void + { + $settings = $event->getSettings(); + + if (!array_key_exists('value', $settings)) { + return; + } + + if (!is_a($settings['value'], CustomerGroup::class)) { + return; + } + + /** @var object $owner */ + $owner = $settings['value']; + $settings['value'] = $owner->getId(); + $event->setSettings($settings); + } +} diff --git a/Processors/AutoCustomerGroupAssignment.php b/Processors/AutoCustomerGroupAssignment.php index 0d37a0d..f043dff 100644 --- a/Processors/AutoCustomerGroupAssignment.php +++ b/Processors/AutoCustomerGroupAssignment.php @@ -30,9 +30,9 @@ public function __construct( public function assignGroup($customerId): void { - $customerGroup = $this->getDefaultCustomerGroupAssignment(); - // reload it due to cascade persist error - $customerGroup = $this->manager->getRepository(CustomerGroup::class)->find($customerGroup->getId()); + $customerGroupId = $this->getDefaultCustomerGroupIdAssignment(); + $customerGroup = $this->manager->getRepository(CustomerGroup::class)->find($customerGroupId); + $customer = $this->manager->getRepository(Customer::class)->find($customerId); if ($customer && $customerGroup && $customerGroup instanceof CustomerGroup) { @@ -43,9 +43,9 @@ public function assignGroup($customerId): void /** * TODO: test on OroCommerce EE with multiple websites / organisations */ - protected function getDefaultCustomerGroupAssignment(): ?CustomerGroup + protected function getDefaultCustomerGroupIdAssignment(): int { - return $this->configManager->get(Configuration::getConfigKeyByName(Configuration::ASSIGNMENT_CUSTOMER_GROUP)); + return (int)$this->configManager->get(Configuration::getConfigKeyByName(Configuration::ASSIGNMENT_CUSTOMER_GROUP)); } protected function setGroup(CustomerGroup $group, Customer $customer): void diff --git a/Resources/config/services.yml b/Resources/config/services.yml index bccdd6c..1f41890 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -5,3 +5,10 @@ services: arguments: - '@doctrine.orm.entity_manager' - '@oro_config.manager' + + Diglin\AutoCustomerGroupBundle\EventListener\SystemConfigListener: + arguments: + - "@doctrine" + tags: + - { name: kernel.event_listener, event: oro_config.settings_form_preset, method: onFormPreSetData } + - { name: kernel.event_listener, event: oro_config.settings_before_save.diglin_customer_group.assignment_customer_group, method: onSettingsSaveBefore } From d67971adbf96603804ffc72eb1d09bac71e7f0ea Mon Sep 17 00:00:00 2001 From: julien Date: Mon, 26 Jul 2021 16:28:53 +0200 Subject: [PATCH 2/2] handle null value in config --- Processors/AutoCustomerGroupAssignment.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Processors/AutoCustomerGroupAssignment.php b/Processors/AutoCustomerGroupAssignment.php index f043dff..bc866d4 100644 --- a/Processors/AutoCustomerGroupAssignment.php +++ b/Processors/AutoCustomerGroupAssignment.php @@ -31,6 +31,9 @@ public function __construct( public function assignGroup($customerId): void { $customerGroupId = $this->getDefaultCustomerGroupIdAssignment(); + if(!$customerGroupId) { + return; + } $customerGroup = $this->manager->getRepository(CustomerGroup::class)->find($customerGroupId); $customer = $this->manager->getRepository(Customer::class)->find($customerId); @@ -43,9 +46,9 @@ public function assignGroup($customerId): void /** * TODO: test on OroCommerce EE with multiple websites / organisations */ - protected function getDefaultCustomerGroupIdAssignment(): int + protected function getDefaultCustomerGroupIdAssignment(): ?string { - return (int)$this->configManager->get(Configuration::getConfigKeyByName(Configuration::ASSIGNMENT_CUSTOMER_GROUP)); + return $this->configManager->get(Configuration::getConfigKeyByName(Configuration::ASSIGNMENT_CUSTOMER_GROUP)); } protected function setGroup(CustomerGroup $group, Customer $customer): void