cancel
Showing results for 
Search instead for 
Did you mean: 

I am new in magento! :: I have task to add a custom field('is_vip') in order columns. please help me

I am new in magento! :: I have task to add a custom field('is_vip') in order columns. please help me

Programatically, add a custom field('is_vip') in order. If a customer order value over $150 , the field value will be 1 and in order grid it will show yes.

 
1 REPLY 1

Re: I am new in magento! :: I have task to add a custom field('is_vip') in order columns. please hel

Hello @fahim_ahmed1 

 

Sure, we can help you to add a custom field to the order grid in Magento 2. Please follow the steps below:

  1. Create a module: Create a new module or use an existing one.
  2. Create a new column in sales_order_grid table: You can create a new column in the sales_order_grid table to store the value of your custom field. Run the following SQL command to add a new column: 
    ALTER TABLE sales_order_grid ADD COLUMN custom_field VARCHAR(255) DEFAULT NULL;

Please replace "custom_field" with the name of your custom field and change the data type if necessary.

1) Please add a new column to the order grid: To display the custom field in the order grid, you need to add a new column to the grid. You can do this by creating a new XML file in your module's view/adminhtml/ui_component directory. 

Here's an example:

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="custom_field" class="Vendor\Module\Ui\Component\Listing\Column\CustomField">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">120</item>
                    <item name="align" xsi:type="string">right</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Custom Field</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

 

2) Replace "Vendor\Module" with the namespace and module name of your module, and "CustomField" with the name of your column class.

You can also customize the column label, data type, and other attributes as needed.

<?php
namespace Vendor\Module\Ui\Component\Listing\Column;
use Magento\Ui\Component\Listing\Columns\Column;
class CustomField extends Column
{
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {
                $item['custom_field'] = $this->getCustomFieldValue($item['entity_id']);
            }
        }
        return $dataSource;
    }
    protected function getCustomFieldValue($orderId)
    {
        // Get the value of your custom field for the given order ID
    }
}

Replace "Vendor\Module" with the namespace and module name of your module, and customize the getCustomFieldValue method to return the value of your custom field for the given order ID.

 

That's it! After completing these steps, you should see your custom field in the order grid in Magento 2.

 

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9