0

I wish to override Magento\Customer\Api\Data\GroupInterface to add my custom fields.

Custom fields are 1.custom_terms 2.handling_fee

I have created the below file in etc/extension_attribues.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\GroupInterface">
        <attribute code="custom_terms" type="string"/>
        <attribute code="handling_fee" type="string"/>
    </extension_attributes>

</config>

I have created two additional columns to save my extension attribute values in a customer_group table enter image description here

My Controller

<?php

namespace PPC\CustomerGroup\Controller\Adminhtml\Group;

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

class Save extends \Magento\Customer\Controller\Adminhtml\Group\Save
{
    protected $groupExtensionInterfaceFactory;
    public function __construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry,
                                GroupRepositoryInterface $groupRepository,
                                GroupInterfaceFactory $groupDataFactory,
                                \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
                                \Magento\Framework\View\Result\PageFactory $resultPageFactory,
                                \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
                                \Magento\Customer\Api\Data\GroupExtensionInterfaceFactory $groupExtensionInterfaceFactory)
    {
        $this->groupExtensionInterfaceFactory = $groupExtensionInterfaceFactory;
        parent::__construct($context, $coreRegistry, $groupRepository, $groupDataFactory, $resultForwardFactory, $resultPageFactory, $dataObjectProcessor);
    }

    public function execute()
    {
        $taxClass = (int)$this->getRequest()->getParam('tax_class');
        $customTerm = $this->getRequest()->getParam('custom_terms');
        $handlingFee = $this->getRequest()->getParam('handling_fee');

        /** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */
        $customerGroup = null;
        if ($taxClass) {
            $id = $this->getRequest()->getParam('id');
            $resultRedirect = $this->resultRedirectFactory->create();
            try {
                $customerGroupCode = (string)$this->getRequest()->getParam('code');
                if ($id !== null) {
                    $customerGroup = $this->groupRepository->getById((int)$id);
                    $customerGroupCode = $customerGroupCode ?: $customerGroup->getCode();
                } else {
                    $customerGroup = $this->groupDataFactory->create();
                }
                $extensionAttributes = $customerGroup->getExtensionAttributes();
                $customerExtension = $extensionAttributes ? $extensionAttributes : $this->groupExtensionInterfaceFactory->create();
                $customerExtension->setCustomTerms($customTerm);
                $customerExtension->setHandlingFee($handlingFee);
                $customerGroup->setCode(!empty($customerGroupCode) ? $customerGroupCode : null);
                $customerGroup->setTaxClassId($taxClass);
                $customerGroup->setExtensionAttributes($customerExtension);

                $this->groupRepository->save($customerGroup);

                $this->messageManager->addSuccess(__('You saved the customer group.'));
                $resultRedirect->setPath('customer/group');
            } catch (\Exception $e) {
                $this->messageManager->addError($e->getMessage());
                if ($customerGroup != null) {
                    $this->storeCustomerGroupDataToSession(
                        $this->dataObjectProcessor->buildOutputDataArray(
                            $customerGroup,
                            \Magento\Customer\Api\Data\GroupInterface::class
                        )
                    );
                }
                $resultRedirect->setPath('customer/group/edit', ['id' => $id]);
            }
            return $resultRedirect;
        } else {
            return $this->resultForwardFactory->create()->forward('new');
        }
    }
}

So my problems are below

  1. How do I check whether the extension attributes are created or not?
  2. The values are not saved in the customer_group table. Can anyone guide me if I missed anything?
  • Did you try to save custom values using plugin? – Kamlesh Solanki Apr 24 at 6:48
1

I hope you have created two fields like custom_terms and handling_fee.

Vendor\Module\Plugin\SavePlugin.php

use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Customer\Model\GroupFactory;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\FilterBuilder;

class SavePlugin {

  protected $_filterBuilder;
  protected $_groupFactory;
  protected $_groupRepository;
  protected $_searchCriteriaBuilder;

  public function __construct(FilterBuilder $filterBuilder,GroupRepositoryInterface $groupRepository, SearchCriteriaBuilder $searchCriteriaBuilder, GroupFactory $groupFactory)
  {
    $this->_filterBuilder = $filterBuilder;
    $this->_groupRepository = $groupRepository;
    $this->_searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->_groupFactory = $groupFactory;
  }       

  public function afterexecute(\Magento\Customer\Controller\Adminhtml\Group\Save $save, $result)
  {   
    $customTerms = $save->getRequest()->getParam('custom_terms');
    $handlingFee = $save->getRequest()->getParam('handling_fee');  
    $code = $save->getRequest()->getParam('code'); 
    if(empty($code))
      $code = 'NOT LOGGED IN';
    $_filter = [ $this->_filterBuilder->setField('customer_group_code')->setConditionType('eq')->setValue($code)->create() ];
    $customerGroups = $this->_groupRepository->getList($this->_searchCriteriaBuilder->addFilters($_filter)->create())->getItems();
    $customerGroup = array_shift($customerGroups);
    if($customerGroup){
     $group = $this->_groupFactory->create();
     $group->load($customerGroup->getId());
     $group->setCode($customerGroup->getCode());
     $group->setData('custom_terms',$customTerms);
     $group->setData('handling_fee',$handlingFee);
     $group->save();
    }
    return $result;
  }       
}

Please try above code to save data.

  • Hi, I have already saved the fields. But It failed to show or prefilled with the saved values while doing edit – Jaisa Apr 24 at 7:30

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.