Adding a new Customer Group Programatically
-
30-03-2021 - |
Question
I would like to know if it is possible from a public function to create a customer group? I would like, in my saveNewGroup function to crate a new customer group. I saw another example online but it was in a setup class and i cannot get it work
This is my class :
<?php
namespace XX\XX\Controller\Adminhtml\Index;
class SaveNewCustomerGroup extends \Magento\Backend\App\Action
{
protected $_resource;
protected $_productRepository;
protected $_customerGroupsCollection;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\ResourceConnection $resource,
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Customer\Model\ResourceModel\Group\Collection $customerGroupsCollection
) {
$this->_resource = $resource;
$this->_productRepository = $productRepository;
$this->_customerGroupsCollection = $customerGroupsCollection;
parent::__construct($context);
}
//Every time the controller is called this function is executed
public function execute(){
$groupName = $this->getRequest()->getParam('customergroup_name');
$taxId = $this->getRequest()->getParam('customergroup_taxid');
echo json_encode($this->saveNewGroup($groupName, $taxId));
}
private function saveNewGroup($groupName, $taxId){
if($groupName == null || $groupName == ""){
return 'Error: Please enter a group name.';
}
else{
return $groupName . " has been created successfully.";
}
}
}
?>
Solution
try out the below code.
<?php
namespace XX\XX\Controller\Adminhtml\Index;
use Magento\Customer\Api\Data\GroupInterfaceFactory as GroupFactory;
use Magento\Customer\Api\GroupRepositoryInterface as GroupRepository;
class SaveNewCustomerGroup extends \Magento\Backend\App\Action
{
protected $_resource;
protected $_productRepository;
protected $_customerGroupsCollection;
protected $_groupFactory;
protected $_groupRepository;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\ResourceConnection $resource,
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Customer\Model\ResourceModel\Group\Collection $customerGroupsCollection,
GroupFactory $groupFactory,
GroupRepository $groupRepository
) {
$this->_resource = $resource;
$this->_productRepository = $productRepository;
$this->_customerGroupsCollection = $customerGroupsCollection;
$this->_groupFactory = $groupFactory;
$this->_groupRepository = $groupRepository;
parent::__construct($context);
}
//Every time the controller is called this function is executed
public function execute() {
try {
$groupName = $this->getRequest()->getParam('customergroup_name');
$taxId = $this->getRequest()->getParam('customergroup_taxid');
if($groupName == null || $groupName == "" ) {
return 'Error: Please enter a group name.';
}
if($taxId == null || $taxId == "" ) {
return 'Error: Please enter a taxId.';
}
// Create the new customer group
$group = $this->_groupFactory->create();
$group->setCode($groupName)
->setTaxClassId( $taxId);
/** vendor/magento/module-customer/Api/GroupRepositoryInterface */
$this->_groupRepository->save($group);
echo json_encode( "Customer Group ". $groupName . " has been created successfully." );
} catch(Exception $e) {
return json_encode($e->getMessage());
exit();
}
}
/*private function saveNewGroup($groupName) {
return $groupName . " has been created successfully.";
}*/
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange