2

I create new extention and add some columns in customer_group table. but i can't get/set custom field.



namespace [NameSpace]\[Extension]\Block\Adminhtml\Group\Edit;

use Magento\Customer\Controller\RegistryConstants;
use Magento\Tax\Model\TaxClass\Source\Product as TaxProduct;

class Form extends \Magento\Customer\Block\Adminhtml\Group\Edit\Form
{

    protected $_taxProduct;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Tax\Model\TaxClass\Source\Customer $taxCustomer,
        \Magento\Tax\Model\TaxClass\Source\Product $taxProduct,
        \Magento\Tax\Helper\Data $taxHelper,
        \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
        \Magento\Customer\Api\Data\GroupInterfaceFactory $groupDataFactory,
        array $data = []
    ) {
        $this->_taxCustomer     = $taxCustomer;
        $this->_taxProduct      = $taxProduct;
        $this->_taxHelper       = $taxHelper;
        $this->_groupRepository = $groupRepository;
        $this->groupDataFactory = $groupDataFactory;
        parent::__construct($context, $registry, $formFactory, $taxCustomer, $taxHelper, $groupRepository, $groupDataFactory, $data);
    }

    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        $form = $this->getForm();
        $groupId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_GROUP_ID);
        /** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */

        if ($groupId === null) {
            $customerGroup = $this->groupDataFactory->create();
        } else {
            $customerGroup      = $this->_groupRepository->getById($groupId);
        }

        $fieldset = $form->addFieldset('tax_classes', ['legend' => __('Tax Classes')]);

        $fieldset->addField(
            'tax_classes_shipping_tax_class',
            'select',
            [
                'name' => 'tax_classes_shipping_tax_class',
                'label' => __('Tax Class for Shipping'),
                'title' => __('Tax Class for Shipping'),
                'class' => '',
                'required' => false,
                'values' => $this->_taxProduct->toOptionArray(),
            ]
        );

        if($this->_backendSession->getCustomerGroupData()) {
            $form->addValues($this->_backendSession->getCustomerGroupData());
            $this->_backendSession->setCustomerGroupData(null);
        } else {
            /* print_r($customerGroup->getData());exit; */
            $form->addValues($customerGroup->getData());
        }
    }
}


namespace [NameSpace]\[Extension]\Controller\Adminhtml\Group;

class Save extends \Magento\Customer\Controller\Adminhtml\Group\Save
{
    /**
     * Create or save customer group.
     *
     * @return \Magento\Backend\Model\View\Result\Redirect|\Magento\Backend\Model\View\Result\Forward
     */
    public function execute()
    {
        $taxClass = (int)$this->getRequest()->getParam('tax_class');

        /** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */
        $customerGroup = null;
        if ($taxClass) {
            $id = $this->getRequest()->getParam('id');
            $resultRedirect = $this->resultRedirectFactory->create();
            try {
                if ($id !== null) {
                    $customerGroup = $this->groupRepository->getById((int)$id);
                } else {
                    $customerGroup = $this->groupDataFactory->create();
                }
                $customerGroupCode = (string)$this->getRequest()->getParam('code');
                if (empty($customerGroupCode)) {
                    $customerGroupCode = null;
                }

                $customerGroup->setCode($customerGroupCode);
                $customerGroup->setTaxClassId($taxClass);
                $data   = $this->getRequest()->getParams();
                $customerGroup->setData('tax_classes_shipping_tax_class',1);
                $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'
                        )
                    );
                }
                $resultRedirect->setPath('customer/group/edit', ['id' => $id]);
            }
            return $resultRedirect;
        } else {
            return $this->resultForwardFactory->create()->forward('new');
        }
    }
}

I try

$customerGroup->getData() or  $customerGroup->getCustomField() 
and

$customerGroup->setData('custom_field',1); 
        $this->_groupRepository->save($customerGroup); 

but fail.

  • 2
    can you please provide your code ? – Jaimin Parikh Jul 18 '16 at 4:43
  • i updated question. what miss here? Thank for help. – Dang Chung Duc Jul 18 '16 at 8:15
  • why you save the file value at \Magento\Customer\Block\Adminhtml\Group\Edit\Form class... it not good idea – Amit Bera Jul 18 '16 at 8:26
  • No. I try save \Magento\Customer\Controller\Adminhtml\Group\Save::execute(). And i want $form->addValues($customerGroup->getData()); but fail. – Dang Chung Duc Jul 18 '16 at 8:30
  • Please share fulll code of` Form` and \Magento\Customer\Controller\Adminhtml\Group\Save: – Amit Bera Jul 18 '16 at 8:32
0

Magento till use Magic setter & getter method for save data of model.

namespace [YourNameSpace];

..... 

use Magento\Customer\Api\Data\GroupInterface;

...

class [Your_Class] extends [Your_Extend] {
 protected $groupRepository;
 public function __construct(   ....
     GroupRepositoryInterface $groupRepository,     ......
 ) {    .....   
     parent::__construct(
        .....
        $groupRepository,
        .......
     );     
     $this->groupRepository = $groupRepository;
 }

 public function saveCustomfield()
 {  
    $id = 'Customer_Group_id';       
    $customerGroup = $this->groupRepository->getById((int)$id);

    $customerGroup->setData('fieldName',$your_Valur);

    $this->groupRepository->save($customerGroup->getCode());    
 }  
}
  • I try and get error "Recoverable Error: Argument 1 passed to Magento\Customer\Model\ResourceModel\GroupRepository\Interceptor::save() must implement interface Magento\Customer\Api\Data\GroupInterface, string given" – Dang Chung Duc Jul 18 '16 at 8:18
  • @DangChungDuc , clear your generated directory – CompactCode Mar 29 '18 at 10:31

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.