Skip to content

Commit

Permalink
Merge pull request #1 from programgames/master
Browse files Browse the repository at this point in the history
fix custom group configuration
  • Loading branch information
sylvainraye authored Jul 26, 2021
2 parents fa5e1d4 + d67971a commit 566b346
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 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);
}
}
11 changes: 7 additions & 4 deletions Processors/AutoCustomerGroupAssignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ 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();
if(!$customerGroupId) {
return;
}
$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,7 +46,7 @@ public function assignGroup($customerId): void
/**
* TODO: test on OroCommerce EE with multiple websites / organisations
*/
protected function getDefaultCustomerGroupAssignment(): ?CustomerGroup
protected function getDefaultCustomerGroupIdAssignment(): ?string
{
return $this->configManager->get(Configuration::getConfigKeyByName(Configuration::ASSIGNMENT_CUSTOMER_GROUP));
}
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 566b346

Please sign in to comment.