Adding a column to the promotions grid in Magento
In this article, we will see how to add a new column to the promotion rules grid (cart rules grid) in Magento.
What is the cart rules grid in Magento?
The cart rules grid displays all the cart rules that were added in Magento. Cart rules are promotions that are applied to the customer's cart. These promotions can have various conditions and logic.
Go to "Marketing --> Cart Price Rules." You will see the cart rules grid:
How to add a new column to the cart rules grid?
To do that, we will need to create a simple Magento plugin (extension). Let's create a plugin that adds the "Times Used" column to the promotions grid. This column can be handy as a reporting tool for information on the usage of discounts.
Plugin creation
Let's first create the standard plugin files in the app/code/Mexbs/AddColumnInSalesRulesGrid folder in our Magento installation:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Mexbs_AddColumnInSalesRulesGrid" setup_version="1.0.0" />
</config>
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mexbs_AddColumnInSalesRulesGrid',
__DIR__
);
Layout file creation
Now, let's create the layout xml file that will define the new column.
<?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>
<referenceContainer name="content">
<referenceBlock name="adminhtml.block.promo.quote.grid.container">
<referenceBlock name="adminhtml.block.promo.quote.grid">
<referenceBlock name="adminhtml.promo.quote.grid.columnSet">
<block class="Magento\Backend\Block\Widget\Grid\Column" name="mexbs.add.column.times_used" as="times_used">
<arguments>
<argument name="header" xsi:type="string" translate="true">Times Used</argument>
<argument name="index" xsi:type="string">times_used</argument>
</arguments>
</block>
</referenceBlock>
</referenceBlock>
</referenceBlock>
</referenceContainer>
</body>
</page>
Installing the module
Now, we need to run the usual commands to enable the module, compile the code, etc:
php bin/magento module:enable Mexbs_AddColumnInSalesRulesGrid
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush
Testing the module
To test our plugin, let's go to the promotion rules grid by going to "Marketing --> Cart Rules." We can see that the column "Times Used" is showing up in the grid.
To sum up
In this article, we showed how to add a column to the cart rules (promotions) grid.
This code was tested on Magento CE 2.4.7-p3