0

We currently have a registration form but we want to change one of the checkboxes to give it additional functionality so that it changes the group the customer will be put into when they register from the default one to a special loyalty one.

I've looked at a few solutions online but I haven't been able to find one that does this exactly, most seem to create a dropdown to select customer group but we just want this one checkbox to change it.

2
  • ok, so which customer group you want to select while tick on checkbox ?
    – Dotsquares
    Jul 18, 2022 at 14:03
  • @Dotsquares when the checkbox is selected we want it to select the group called loyalty which is group id 4 and if it's not then it should put them into the default group.
    – user106062
    Jul 18, 2022 at 14:14

1 Answer 1

0

If you want to change customer group during registration by some request checkbox you can:

Create plugin for \Magento\Customer\Api\AccountManagementInterface after method createAccount and on update $customer group based on \Magento\Framework\App\RequestInterface

Or create observer on event customer_register_success and update customer group.

You don't need to install any additional extension for this. If you have an issue with creating plugin or observer, I can prove the example. Let me know

[UPDATE]

In my example Acme like vendor name, StackExchange - module name

1. Register extension

app/code/Acme/StackExchange/registration.php

<?php
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Acme_StackExchange', __DIR__);

2. Create module declaration

app/code/Acme/StackExchange/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Acme_StackExchange">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

3. Create Dependency Injection for set plugin

app/code/Acme/StackExchange/etc/di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Api\AccountManagementInterface">
        <plugin name="Acme_StackExchange_Plugin_Customer_Api_AccountManagementPlugin"
                type="Acme\StackExchange\Plugin\Customer\Api\AccountManagementPlugin" sortOrder="10"/>
    </type>
</config>

4. Create plugin

app/code/Acme/StackExchange/Plugin/Customer/Api/AccountManagementPlugin.php

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Plugin\Customer\Api;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\App\RequestInterface;

class AccountManagementPlugin
{
    protected RequestInterface            $request;
    protected CustomerRepositoryInterface $customerRepository;

    public function __construct(
        RequestInterface $request,
        CustomerRepositoryInterface $customerRepository
    ) {
        $this->request = $request;
        $this->customerRepository = $customerRepository;
    }

    public function afterCreateAccount(
        AccountManagementInterface $subject,
        CustomerInterface $result
    ): CustomerInterface {
        // check if checkbox value set
        if ($this->request->getParam('your_checkbox_name')) {
            // set custom group
            $result->setGroupId('GROUP_ID');
            // save customer data
            $this->customerRepository->save($result);
        }

        return $result;
    }
}

5. Run setup upgrade

php bin/magento setup:upgrade

References:

3
  • Hi, sorry I'm new when it comes to Magento, so I don't know how to make plugins or observers - but I'm willing to learn. I've seen a lot of people talking about observers in other threads but not seen anyone say where they go though
    – user106062
    Jul 18, 2022 at 15:36
  • You can find a lot of examples here. See updated answer and hope this will help Jul 18, 2022 at 18:25
  • How this works in graphQL? I am using createCustomer mutation.
    – Manjunath
    Apr 15, 2023 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.