I want to display coupon codes in the sales order grid and the order detail page. Any idea how to do this?

2 Answers 2


Follow below steps.

  1. Add below code in di.xml
  1. Create new column in sales order grid table by db_schema.sql file

  2. Create sales_order_grid.xml at app/code/Vendor/Module/view/adminhtml/ui_component

Magento_Ui/js/grid/columns/column text true text left Coupon Code

  1. For one time updates Run below query

    update sales_order_grid set coupon_code=(select coupon_code from sales_order where sales_order.entity_id=sales_order_grid.entity_id)


Try below code here is the sales order grid show coupon code.

  1. Override the Order Grid: In the module, you need to override the Order Grid by creating a new column to display the coupon code. To do this, create a new file in the path Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml and add the below code.

Here is the code.

<column name="coupon_code" class="Vendor\Module\Ui\Component\Listing\Column\CouponCode">
   <argument name="data" xsi:type="array">
      <item name="config" xsi:type="array">
         <item name="filter" xsi:type="string">text</item>
         <item name="label" translate="true" xsi:type="string">Coupon Code</item>
  1. Create a Column File: Now, create a new file in the path Vendor/Module/Ui/Component/Listing/Column/CouponCode.php and add the below code.

Here is the code.

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

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface;

class CouponCode extends \Magento\Ui\Component\Listing\Columns\Column
   protected $_orderCollectionFactory;
   protected $dateTimeFormatter;

   public function __construct(
      ContextInterface $context,
      UiComponentFactory $uiComponentFactory,
      CollectionFactory $orderCollectionFactory,
      DateTimeFormatterInterface $dateTimeFormatter,
      array $components = [],
      array $data = []
   ) {
      $this->_orderCollectionFactory = $orderCollectionFactory;
      $this->dateTimeFormatter = $dateTimeFormatter;
      parent::__construct($context, $uiComponentFactory, $components, $data);

   public function prepareDataSource(array $dataSource)
      if (isset($dataSource['data']['items'])) {
         $fieldName = $this->getData('name');
         foreach ($dataSource['data']['items'] as &$item) {
            $order = $this->_getOrder($item["entity_id"]);
            if($order->getCouponCode()) {
               $item[$fieldName] = $order->getCouponCode();
            else {
               $item[$fieldName] = '-';
      return $dataSource;

   private function _getOrder($id)
      $collection = $this->_orderCollectionFactory->create()->addFieldToSelect('*');
      return $collection->getFirstItem();

And try below code for sales order detail.

In the module, create a new file in the following path: app/code/<Vendor>/<Module>/view/adminhtml/templates/sales/order/view/info.phtml

    $couponCode = $block->getOrder()->getCouponCode();
    if ($couponCode) {
        echo __('Coupon Code: %1', $couponCode);

Let me know if any query.


  • This solution provided a bad performance.
    – HoangHieu
    Feb 15, 2023 at 6:57
  • Can you provide best performance solution? Feb 15, 2023 at 6:59
  • You should add the coupon_code to the table sales_order_grid by adding that column to the sales order grid insert sales_grid_order_async_insert
    – HoangHieu
    Feb 15, 2023 at 13:55

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.