Adding Custom Columns To Invoice/creditmemo/shipment Grids

by ADMIN 59 views

Introduction

In Magento 2.4.5, the way custom columns are added to invoice, creditmemo, and shipment grids has changed significantly. If you have updated your Magento 2 from 2.3.3 to 2.4.5 and your old custom module no longer works, this article will guide you through the process of adding custom columns to these grids.

Understanding the Changes

In Magento 2.4.5, the sales_order_grid table has been replaced by sales_order_grid_flat table. This change affects how custom columns are added to the grid. The old method of using a Left Join from the sales_order table is no longer supported.

New Method: Using a Plugin

To add custom columns to the invoice, creditmemo, and shipment grids in Magento 2.4.5, you need to use a plugin. A plugin is a way to extend the functionality of a module without modifying its code.

Step 1: Create a Plugin

To create a plugin, you need to create a new module in your Magento 2 installation. The module should have a etc/di.xml file that defines the plugin.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\ResourceModel\Order\Grid\Collection">
        <plugin name="custom_columns" type="Vendor\Module\Plugin\Sales\Order\Grid\Collection" />
    </type>
</config>

Step 2: Create the Plugin Class

The plugin class should extend the Magento\Sales\Model\ResourceModel\Order\Grid\Collection class.

namespace Vendor\Module\Plugin\Sales\Order\Grid;

use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;

class Collection extends OriginalCollection { public function addCustomColumn($column) { this-&gt;addColumn(column); } }

Step 3: Add the Custom Column

To add the custom column, you need to create a new method in the plugin class that adds the column to the grid.

public function addCustomColumn($column)
{
    $this->addColumn($column);
}

Step 4: Define the Custom Column

To define the custom column, you need to create a new class that extends the Magento\Ui\Component\Listing\Columns\Column class.

namespace Vendor\Module\Ui\Component\Listing\Column;

use Magento\Ui\Component\Listing\Columns\Column;

class CustomColumn extends Column { public function prepareDataSource(array dataSource) { if (isset(dataSource['data']['items'])) { foreach (dataSource[&#39;data&#39;][&#39;items&#39;] as &amp;item) { item[item[this->getData('name')] = this-&gt;getValue(item); } } return $dataSource; } }

Step 5: Register the Custom Column

To register the custom column, you need to add a new entry the etc/ui_component.xml file.

<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_component.xsd">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
            <item name="filterable" xsi:type="boolean">true</item>
            <item name="sortable" xsi:type="boolean">true</item>
            <item name="hidable" xsi:type="boolean">true</item>
            <item name="index" xsi:type="string">custom_column</item>
        </item>
    </argument>
    <columns name="custom_columns">
        <column name="custom_column" xsi:type="vendor_module_ui_component_listing_column_customcolumn">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Custom Column</item>
                    <item name="sort_order" xsi:type="number">100</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Conclusion

Adding custom columns to the invoice, creditmemo, and shipment grids in Magento 2.4.5 requires a plugin. The plugin should extend the Magento\Sales\Model\ResourceModel\Order\Grid\Collection class and add a new method that adds the custom column to the grid. The custom column should be defined in a new class that extends the Magento\Ui\Component\Listing\Columns\Column class. The custom column should be registered in the etc/ui_component.xml file.

Troubleshooting

If you encounter any issues while adding custom columns to the invoice, creditmemo, and shipment grids, you can try the following:

  • Check the plugin configuration in the etc/di.xml file.
  • Check the plugin class and ensure that it extends the correct class.
  • Check the custom column class and ensure that it extends the correct class.
  • Check the custom column registration in the etc/ui_component.xml file.

Example Use Case

To add a custom column to the invoice grid, you can use the following code:

$collection = \Magento\Sales\Model\ResourceModel\Order\Grid\Collection::create();
$collection->addCustomColumn('custom_column');

This code creates a new instance of the Magento\Sales\Model\ResourceModel\Order\Grid\Collection class and adds a custom column named custom_column to the grid.

Commit Message

If you are committing the code to a version control system, you can use the following commit message:

Added custom column to invoice grid

Q: What is the difference between Magento 2.3.3 and Magento 2.4.5?

A: Magento 2.4.5 has made significant changes to the way custom columns are added to invoice, creditmemo, and shipment grids. The sales_order_grid table has been replaced by sales_order_grid_flat table, which affects how custom columns are added to the grid.

Q: Why is my old custom module not working in Magento 2.4.5?

A: Your old custom module is not working in Magento 2.4.5 because it uses the old method of adding custom columns to the grid, which is no longer supported. You need to update your module to use the new method of adding custom columns.

Q: What is a plugin in Magento?

A: A plugin in Magento is a way to extend the functionality of a module without modifying its code. Plugins are used to add custom columns to the grid.

Q: How do I create a plugin in Magento?

A: To create a plugin in Magento, you need to create a new module and add a etc/di.xml file that defines the plugin.

Q: What is the etc/di.xml file?

A: The etc/di.xml file is a configuration file that defines the plugin. It specifies the class that the plugin should extend and the method that the plugin should call.

Q: How do I add a custom column to the grid using a plugin?

A: To add a custom column to the grid using a plugin, you need to create a new method in the plugin class that adds the custom column to the grid.

Q: What is the addCustomColumn method?

A: The addCustomColumn method is a method in the plugin class that adds a custom column to the grid. It takes the name of the custom column as an argument and adds it to the grid.

Q: How do I define a custom column?

A: To define a custom column, you need to create a new class that extends the Magento\Ui\Component\Listing\Columns\Column class.

Q: What is the prepareDataSource method?

A: The prepareDataSource method is a method in the custom column class that prepares the data source for the custom column.

Q: How do I register a custom column?

A: To register a custom column, you need to add a new entry to the etc/ui_component.xml file.

Q: What is the etc/ui_component.xml file?

A: The etc/ui_component.xml file is a configuration file that defines the custom column. It specifies the name of the custom column, the label, and the sort order.

Q: Can I add multiple custom columns to the grid?

A: Yes, you can add multiple custom columns to the grid by creating multiple plugin classes and adding them to the grid.

Q: How do I troubleshoot issues with custom columns?

A: To troubleshoot issues with custom columns, you can check the plugin configuration in the etc/di.xml file, the plugin class, the custom column class, and the custom column registration in the etc/ui_component.xml file.

Q: What is the best practice for adding custom columns to the grid?

A: The best practice for adding custom columns to the grid is to create a new plugin class for each custom column and add it to the grid using the addCustomColumn method.