5

I'd like to know if it's possible (if so, how) to programmatically create a new customer group using the Magento 2 framework. Ideally I'd like to do this in a setup class.

Looking at the GroupRegistry class, I only see a retrieve() and remove() method. The class also doesn't extend anything so wouldn't inherit these methods - so I know this isn't the right place to look.

Of course, I'm probably looking in the wrong place or misunderstand the concept of how it works.

Do I even need to bother trying to find the class that will handle this, or should I just specify a GroupRepositoryInterface in the constructor of the class and assume it'll be provided by the object manager? The interface provides the save() method, which is what I need.

This is what I'm thinking so far:

namespace MyCompany\MyModule\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Customer\Model\GroupFactory;

class InstallData implements InstallDataInterface
{
    protected $groupFactory;
    protected $groupRepository;

    /**
     * I'd like a customer group factory and a customer group repository
     */
    public function __construct(
        GroupFactory $groupFactory,
        GroupRepositoryInterface $groupRepository
    ) {
        $this->groupFactory = $groupFactory;
        $this->groupRepository = $groupRepository;
    }

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

        // Create the new group
        $group = $this->groupFactory->create();

        $group->setCode('My New Group');
        $group->setTaxClassId(3); // magic number is only for demo..

        // Use the group repository to save it
        $this->groupRepository->save($group);

        $setup->endSetup();
    }
}

Am I close? If I run this, I get the following exception:

Argument 1 passed to Magento\Customer\Model\ResourceModel\GroupRepository\Interceptor::save() must implement interface Magento\Customer\Api\Data\GroupInterface, instance of Magento\Customer\Model\Group\Interceptor given

  • how many groups are you creating. – encodes Feb 24 '16 at 13:38
  • @encodes in this scenario it was one, but assume any number - question was around the concept of creating them in general – Robbie Averill Feb 24 '16 at 20:49
13

May as well answer this since I figured it out.

The problem with my example above is that I was trying to use the GroupRepository to push the new model to the database.

Instead, I should have just used $group->save() on the model returned by the GroupFactory:

<?php
namespace MyCompany\MyModule\Setup;

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

class InstallData implements InstallDataInterface
{
    protected $groupFactory;

    /**
     * I'd like a customer group factory please Sir!
     */
    public function __construct(GroupFactory $groupFactory) {
        $this->groupFactory = $groupFactory;
    }

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

        // Create the new group
        /** @var \Magento\Customer\Model\Group $group */
        $group = $this->groupFactory->create();
        $group
            ->setCode('My New Group')
            ->setTaxClassId(3) // magic numbers OK, core installers do it?!
            ->save();

        $setup->endSetup();
    }
}
  • I am also facing the same issue. But i am already saving the group without repository. But when i try to inspect the code Magento gives the warning that i am using the deprecated function save. And during taking the Magento U courses they explicitly mention in future save methods of the models can be removed. So is it fine to use the save method of the group instead save using repository? – Abbas Jan 31 '17 at 0:59
  • Hi @Abbas, the public API may have changed since I wrote this post - you probably know better than I do! If you could post a link to any references you have about ->save() being deprecated, that'd be awesome. – Robbie Averill Jan 31 '17 at 1:24
  • 1
    There's a syntax error with your code, you write ->setCode('My New Group'); then have ->setTaxClassId(3) on the next line. The semicolon closes the $group variable, but then no variable is defined for the next line. You should take the semicolon out - I'll make an edit if I can for you. – hazrpg Sep 4 '17 at 14:50
  • The save is deprecated – Mohamed El Mrabet Sep 10 '18 at 8:39
  • @MohamedElMrabet yes, see the answer below – Robbie Averill Sep 10 '18 at 9:04
5

Since the version 2.2.1 the use of the ->save() method from the Magento\Customer\Model\Group implementations has been deprecated.

Instead, you should use:

use Magento\Customer\Api\Data\GroupInterfaceFactory;
use Magento\Customer\Api\GroupRepositoryInterface;

instead

use Magento\Customer\Model\GroupFactory;

Using Robbie Averill solution as a baseline: https://magento.stackexchange.com/a/93789/15530

Pass those classes to the constructor like:

public function __construct(
        GroupInterfaceFactory $groupFactory,
        GroupRepositoryInterface $groupInterface) {
        $this->groupFactory = $groupFactory;
        $this->groupInterface = $groupInterface;
}

And then just use it as per this example: $this->groupInterface->save($group);

Where group is created using the factory creator like: $group = $this->groupFactory->create();

4

Save method is deprecated in Magento 2 try this code:

<?php
/**
 * Copyright © 2018 MyCompany. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace MyCompany\MyModule\Setup;

use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Customer\Api\Data\GroupInterfaceFactory;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Class InstallData
 *
 * @package MyCompany\MyModule\Setup
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var GroupInterfaceFactory
     */
    private $groupFactory;

    /**
     * @var GroupRepositoryInterface
     */
    private $groupRepository;

    /**
     * InstallData constructor.
     *
     * @param GroupInterfaceFactory $groupFactory
     * @param GroupRepositoryInterface $groupRepository
     */
    public function __construct(
        GroupInterfaceFactory $groupFactory,
        GroupRepositoryInterface $groupRepository
    ) {
        $this->groupFactory = $groupFactory;
        $this->groupRepository = $groupRepository;
    }

    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        try {
            // Create the new group
            /** @var \Magento\Customer\Model\Group $group */
            $group = $this->groupFactory->create();
            $group
                ->setCode('My New Group')
                ->setTaxClassId(3);
            $this->groupRepository->save($group);
        } catch (\Exception $e) {

        }

        $setup->endSetup();
    }
}
  • This doesn't provide anything more than this existing answer does – Robbie Averill Sep 10 '18 at 9:05
  • Ok but the code is more clear, i dont see this answer before i write mine this code i just tested in personal project – Mohamed El Mrabet Sep 10 '18 at 9:33
  • @MohamedElMrabet, saying Save method is deprecated but you are using still save() in your answer – jafar pinjar Feb 21 at 7:18
0

You can create a single/multiples customer groups by below script code

File Path : app/code/MyCompany/MyModule/Setup/InstallData.php

<?php
namespace MyCompany\MyModule\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Model\GroupFactory;

class InstallData implements InstallDataInterface
{

    protected $groupFactory;

    /**
     * Constructor
     *
     * @param Magento\Customer\Model\GroupFactory $groupFactory
     */
    public function __construct(

        GroupFactory $groupFactory
    ) {

        $this->groupFactory = $groupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {


        /** Create a customer Group */
        /** @var \Magento\Customer\Model\Group $group */
        $setup->startSetup();

    /* Create a multiple customer group */
        $setup->getConnection()->insertForce(
            $setup->getTable('customer_group'),
            ['customer_group_code' => 'YourNewCustomerGroup', 'tax_class_id' => 3]
        );
        $setup->getConnection()->insertForce(
            $setup->getTable('customer_group'),
            ['customer_group_code' => 'YourNewCustomerGroup', 'tax_class_id' => 3]
        );

        $setup->endSetup();
    }
}
  • This is not the incorrect answer, This is just the core magento way to resolved the question. So down-vote such answer is not the proper way. – Ronak Chauhan Sep 29 '17 at 7:44
  • See my answer using the repository $this->groupRepository->save($group); – Mohamed El Mrabet Sep 10 '18 at 8:44
0

Instead of use Model Factory class use Interface because Magento not deprecated all use of Model save methods.

Use below code:

/** @var GroupInterface $group */
$group = $this->groupInterfaceFactory->create();
$group->setCode('Group Name');
$group->setTaxClassId(3); // Tax class id..

// Use the group repository to save it
$this->groupRepositoryInterface->save($group);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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