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.
\Magento\Customer\Block\Adminhtml\Group\Edit\Form
class... it not good idea\Magento\Customer\Controller\Adminhtml\Group\Save: