Skip to content

Commit 566b346

Browse files
authored
Merge pull request #1 from programgames/master
fix custom group configuration
2 parents fa5e1d4 + d67971a commit 566b346

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Diglin\AutoCustomerGroupBundle\EventListener;
6+
7+
use Doctrine\Persistence\ManagerRegistry;
8+
use Diglin\AutoCustomerGroupBundle\DependencyInjection\AutoCustomerGroupExtension;
9+
use Diglin\AutoCustomerGroupBundle\DependencyInjection\Configuration;
10+
use Oro\Bundle\ConfigBundle\Config\ConfigManager;
11+
use Oro\Bundle\ConfigBundle\Event\ConfigSettingsUpdateEvent;
12+
use Oro\Bundle\CustomerBundle\Entity\CustomerGroup;
13+
14+
class SystemConfigListener
15+
{
16+
protected ManagerRegistry $registry;
17+
18+
protected string $ownerClass;
19+
20+
public function __construct(ManagerRegistry $registry)
21+
{
22+
$this->registry = $registry;
23+
}
24+
25+
public function onFormPreSetData(ConfigSettingsUpdateEvent $event): void
26+
{
27+
$settingsKey = implode(ConfigManager::SECTION_VIEW_SEPARATOR, [AutoCustomerGroupExtension::ALIAS, Configuration::ASSIGNMENT_CUSTOMER_GROUP]);
28+
$settings = $event->getSettings();
29+
if (is_array($settings)
30+
&& !empty($settings[$settingsKey]['value'])
31+
) {
32+
$settings[$settingsKey]['value'] = $this->registry
33+
->getManagerForClass(CustomerGroup::class)
34+
->find(CustomerGroup::class, $settings[$settingsKey]['value']);
35+
$event->setSettings($settings);
36+
}
37+
}
38+
39+
public function onSettingsSaveBefore(ConfigSettingsUpdateEvent $event): void
40+
{
41+
$settings = $event->getSettings();
42+
43+
if (!array_key_exists('value', $settings)) {
44+
return;
45+
}
46+
47+
if (!is_a($settings['value'], CustomerGroup::class)) {
48+
return;
49+
}
50+
51+
/** @var object $owner */
52+
$owner = $settings['value'];
53+
$settings['value'] = $owner->getId();
54+
$event->setSettings($settings);
55+
}
56+
}

Processors/AutoCustomerGroupAssignment.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ public function __construct(
3030

3131
public function assignGroup($customerId): void
3232
{
33-
$customerGroup = $this->getDefaultCustomerGroupAssignment();
34-
// reload it due to cascade persist error
35-
$customerGroup = $this->manager->getRepository(CustomerGroup::class)->find($customerGroup->getId());
33+
$customerGroupId = $this->getDefaultCustomerGroupIdAssignment();
34+
if(!$customerGroupId) {
35+
return;
36+
}
37+
$customerGroup = $this->manager->getRepository(CustomerGroup::class)->find($customerGroupId);
38+
3639
$customer = $this->manager->getRepository(Customer::class)->find($customerId);
3740

3841
if ($customer && $customerGroup && $customerGroup instanceof CustomerGroup) {
@@ -43,7 +46,7 @@ public function assignGroup($customerId): void
4346
/**
4447
* TODO: test on OroCommerce EE with multiple websites / organisations
4548
*/
46-
protected function getDefaultCustomerGroupAssignment(): ?CustomerGroup
49+
protected function getDefaultCustomerGroupIdAssignment(): ?string
4750
{
4851
return $this->configManager->get(Configuration::getConfigKeyByName(Configuration::ASSIGNMENT_CUSTOMER_GROUP));
4952
}

Resources/config/services.yml

+7
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ services:
55
arguments:
66
- '@doctrine.orm.entity_manager'
77
- '@oro_config.manager'
8+
9+
Diglin\AutoCustomerGroupBundle\EventListener\SystemConfigListener:
10+
arguments:
11+
- "@doctrine"
12+
tags:
13+
- { name: kernel.event_listener, event: oro_config.settings_form_preset, method: onFormPreSetData }
14+
- { name: kernel.event_listener, event: oro_config.settings_before_save.diglin_customer_group.assignment_customer_group, method: onSettingsSaveBefore }

0 commit comments

Comments
 (0)