문제

I need to remove the "Group" dropdown field in order to create a form in the backend ?

Can anyone guide me to remove this field in magento2

enter image description here

도움이 되었습니까?

해결책

The code for the part you're looking for is located in the sales_order_create_index.xml file in the layout folder of the backend in module-sales in a vendor folder.

If you want to completely remove Group dropdown and email field then you need to modify ( DO NOT directly modify the vendor file, use overriding mechanism by module or theme ):

ProjectDirectory/vendor/magento/module-sales/view/adminhtml/layout/sales_order_create_index.xml

and then there you need to remove the block:

<block class="Magento\Sales\Block\Adminhtml\Order\Create\Form\Account" template="Magento_Sales::order/create/form/account.phtml" name="form_account"/>

If you want to only remove the Group dropdown, follow below steps:

Create or use your existing module whichever you prefer.

  1. Vendor/Module/etc/di.xml

    <?xml version="1.0"?>
         <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
             <preference for="Magento\Sales\Block\Adminhtml\Order\Create\Form\Account" type="Vendor\Module\Block\Adminhtml\Order\Create\Form\Account" />
     </config>
    
  2. Vendor/Module/Block/Adminhtml/Order/Create/Form/Account.php

     <?php
    
     namespace Vendor\Module\Block\Adminhtml\Order\Create\Form;
    
     use Magento\Framework\View\Element\Template;
    
     class Account extends \Magento\Sales\Block\Adminhtml\Order\Create\Form\Account
     {
         protected function _prepareForm()
         {
             /** @var \Magento\Customer\Model\Metadata\Form $customerForm */
             $customerForm = $this->_metadataFormFactory->create('customer', 'adminhtml_checkout');
    
             // prepare customer attributes to show
             $attributes = [];
    
             // add system required attributes
             foreach ($customerForm->getSystemAttributes() as $attribute) {
                 if( $attribute->getAttributeCode() != 'group_id' ) { //Line added
                     if ($attribute->isRequired()) {
                         $attributes[$attribute->getAttributeCode()] = $attribute;
                     }
                 }
             }
    
             if ($this->getQuote()->getCustomerIsGuest()) {
                 unset($attributes['group_id']);
             }
    
             // add user defined attributes
             foreach ($customerForm->getUserAttributes() as $attribute) {
                 $attributes[$attribute->getAttributeCode()] = $attribute;
             }
    
             $fieldset = $this->_form->addFieldset('main', []);
    
             $this->_addAttributesToForm($attributes, $fieldset);
    
             $this->_form->addFieldNameSuffix('order[account]');
             $this->_form->setValues($this->extractValuesFromAttributes($attributes));
    
             return $this;
         }
         private function extractValuesFromAttributes(array $attributes): array
         {
             $formValues = $this->getFormValues();
             foreach ($attributes as $code => $attribute) {
                 $defaultValue = $attribute->getDefaultValue();
                 if (isset($defaultValue) && !isset($formValues[$code])) {
                     $formValues[$code] = $defaultValue;
                 }
             }
    
             return $formValues;
         }
     }
    

enter image description here

Explanation: What we've done here is, it is automatically binding the attributes so if you remove the complete code it'll also remove email field. So, in order to remove only Group down we have to modify _prepareForm function and added a condition in foreach if( $attribute->getAttributeCode() != 'group_id' ), so it'll skip Group attribute. and since extractValuesFromAttributes function is a private function, we also need it to put it in our preference file as we cannot call private method in _prepareForm from extending class.

다른 팁

If you always want to use General and you are pretty certain that your users don't do bad things the easiest way is just to hide it via CSS.

If you want to "properly" remove it you have to

A) remove it from the ui_component

B) extend the functionality to auto set the group as it as a required value.

This will override and/or extend a lot of core features which will be harder to maintain, therefore i highly suggest the css approach.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top