0

How to add custom input and drop-downs order create in admin sales order creation.

I want to add custom field hopefully a new section where when we create a custom order through admin only we can pass custom values to it.

I am not sure if I am creating a custom attributes.

Please can someone provide me with any tips or where to look

1 Answer 1

0

To add custom fields to the order creation form in the Magento 2 admin area, you can follow these steps:

1.Create a module: Create a custom module in Magento to house your customizations. You can create a new module using the Magento CLI command:

php bin/magento module:create --name=Vendor_ModuleName

2.Define the custom attribute: In your module, create a etc/extension_attributes.xml file to define the custom attribute for the order. For example, if you want to add a custom text field called "Custom Field"

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
        <attribute code="custom_field" type="string" />
    </extension_attributes>
</config>

3.Create the UI component form: Create a UI component form to add the custom field to the order creation page. In your module, create a file at view/adminhtml/ui_component/sales_order_create_form.xml. Here's an example of adding a text field and a dropdown field:

<?xml version="1.0"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="custom_fieldset">
        <field name="custom_field" formElement="input">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Custom Field</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataScope" xsi:type="string">custom_field</item>
                    <item name="sortOrder" xsi:type="number">100</item>
                </item>
            </argument>
        </field>
        <field name="custom_dropdown" formElement="select">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Custom Dropdown</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataScope" xsi:type="string">custom_dropdown</item>
                    <item name="sortOrder" xsi:type="number">110</item>
                    <item name="options" xsi:type="array">
                        <item name="option1" xsi:type="array">
                            <item name="label" xsi:type="string" translate="true">Option 1</item>
                            <item name="value" xsi:type="string">option1</item>
                        </item>
                        <item name="option2" xsi:type="array">
                            <item name="label" xsi:type="string" translate="true">Option 2</item>
                            <item name="value" xsi:type="string">option2</item>
                        </item>
                    </item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

4.Modify the order create layout: Override the sales_order_create_index.xml layout file to include your custom form. Create the file at view/adminhtml/layout/sales_order_create_index.xml in your module and add the following content:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="content">
            <block class="Magento\Sales\Block\Adminhtml\Order\Create" template="Vendor_ModuleName::order/create.phtml" name="order_create">
                <block class="Magento\Sales\Block\Adminhtml\Order\Create\Form" template="Vendor_ModuleName::order/create/form.phtml" name="order_form" as="form">
                    <block class="Magento\Sales\Block\Adminhtml\Order\Create\Data" name="order_data" as="data">
                        <container name="custom_fieldset" label="Custom Fieldset" />
                    </block>
                </block>
            </block>
        </referenceBlock>
    </body>
</page>

5.Create the template files: Create the templates for the form fields. In your module, create the file view/adminhtml/templates/order/create/form.phtml and add the following content:

<?= $block->getChildHtml('data') ?>

Create the file view/adminhtml/templates/order/create.phtml and add the following content:

<?= $block->getChildHtml('form') ?>

6.Run setup and cache commands: Run the following CLI commands in the Magento root directory

php bin/magento setup:upgrade
php bin/magento cache:flush

After completing these steps, you should see the custom fields ("Custom Field" and "Custom Dropdown") in the order creation form in the Magento admin area. You can access the custom field values through the custom_field and custom_dropdown properties of the order object.

Remember to replace "Vendor" with your module's vendor name and "ModuleName" with your module's name.

1
  • it's not showing in admin order create May 31, 2023 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.