Skip to content

Commit

Permalink
fix custom group configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
programgames committed Jul 26, 2021
1 parent fa5e1d4 commit 8f0ea9e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
56 changes: 56 additions & 0 deletions EventListener/SystemConfigListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Diglin\AutoCustomerGroupBundle\EventListener;

use Doctrine\Persistence\ManagerRegistry;
use Diglin\AutoCustomerGroupBundle\DependencyInjection\AutoCustomerGroupExtension;
use Diglin\AutoCustomerGroupBundle\DependencyInjection\Configuration;
use Oro\Bundle\ConfigBundle\Config\ConfigManager;
use Oro\Bundle\ConfigBundle\Event\ConfigSettingsUpdateEvent;
use Oro\Bundle\CustomerBundle\Entity\CustomerGroup;

class SystemConfigListener
{
protected ManagerRegistry $registry;

protected string $ownerClass;

public function __construct(ManagerRegistry $registry)
{
$this->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);
}
}
10 changes: 5 additions & 5 deletions Processors/AutoCustomerGroupAssignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 8f0ea9e

Please sign in to comment.