How to write code to delete customer group and assign the customers in it to default customer group?

magento.stackexchange https://magento.stackexchange.com/questions/306499

  •  12-04-2021
  •  | 
  •  

Question

I have created Customer group using InstallData.php in

app/code/Mymodule/setup

I need to delete it programatically.How should I do it? I added UpgradeData.php inside Setup folder.

<?php

namespace <Mymodule\Customer\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.1.0', '<=')) {
            $setup->getConnection()->query("DELETE FROM customer_group WHERE customer_group_id = '4'");
      //  $sql = "Delete FROM " . $tableName." Where emp_id = 10";
//$connection->query($sql);
        }

        $setup->endSetup();
    }
}

But it is not working.Help me out.

Was it helpful?

Solution

Try below code to remove customer group using upgradeData script:

<?php
namespace YourCompany\YourPackage\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\GroupFactory;

class UpgradeData implements UpgradeDataInterface
{
    protected $groupFactory;

    public function __construct(GroupFactory $groupFactory) {
        $this->groupFactory = $groupFactory;
    }

    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.1', '<=')) {
            // remove customer group
            /** @var \Magento\Customer\Model\Group $group */
            $group = $this->groupFactory->create();
            $group->load(4)
                ->delete();
        }

        $setup->endSetup();
    }
}

You can add another script to assign required customers to default group :)

To update customers group:

create a file with any name like AssignToDefault.php with below code

<?php

use Magento\Framework\App\Bootstrap;

ini_set('display_errors', 1);
require __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$objectManager = $bootstrap->getObjectManager();

$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productionDomains = '';
$developmentDomains = '';
$i = 0;
$customers = $objectManager->create('Magento\Customer\Model\Customer')->getCollection();
foreach ($customers as $record) {
    $customer = $objectManager->create('Magento\Customer\Model\Customer')->load($record->getId());
    $customer->setGroupId(1);
    $customer->save();
}
echo "<br />" . "All Customers Updated successfully!";
?>

Put this file under MagentoRootDirectory/pub folder and execute below command in terminal:

php pub/AssignToDefault.php 

here AssignToDefault.php is the filename in which we added the code to update customer group. I didn't check but you can give a try to this code, I hope it would be working fine. Please let me know if you find any issue..

OTHER TIPS

You can do this from admin but if you want to do this from code then you need to call GroupRepository's deleteById function for this.

public function __construct(\Magento\Customer\Api\GroupRepository $groupRepository)
{
    $this->groupRepo = $groupRepository
}

public function upgrade()
{
   $setup->startSetup();

    if (version_compare($context->getVersion(), '1.0.1', '<=')) {
         $groupId = 5;
         $this->groupRepo->deleteById($groupId)
    }

    $setup->endSetup();

}

Please don't forget to update setup_version to 1.0.1 in module.xml file and run setup upgrade. Let me know if you need any other help.

We should use GroupRepositoryInterface.

<?php
namespace Namespace\Modulename\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{
    protected $groupFactory;

    public function __construct(\Magento\Customer\Api\GroupRepositoryInterface $groupRepositoryInterface) {
        $this->groupRepo = $groupRepositoryInterface;
    }

    public function upgrade(ModuleDataSetupInterface $setup,ModuleContextInterface $context) {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.0', '<=')) {
            $groupId = 5;
            $this->groupRepo->deleteById($groupId);
        }
        $setup->endSetup();
    }

}

I think you don't need any code for delete customer group programmatically,

Just delete customer group from the admin side and all customer which consider that group will automatically be assigned into default general group

Hope it will help you.

Create File in VendorName\ModuleName\Setup\InstallData.php

<?php
namespace VendorName\ModuleName\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * Customer Group Factory Model
     *
     * @var \Magento\Customer\Model\GroupFactory
     */
    protected $groupFactory;
    public function __construct(
        \Magento\Customer\Model\GroupFactory $groupFactory
    ) {
        $this->groupFactory = $groupFactory;
    }

    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();

        /* @var \Magento\Customer\Model\GroupFactory $group */

        $group = $this->groupFactory->create();
        $group->load(customer_group_id);
        $group->delete();

        $setup->endSetup();
    }
}

It will run when you upgrade setup.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top