2

I am trying to add my custom table field to order grid but its not working fine for sales order grid but its working fine product grid so below are the code adding column to sales order grid and product grid

i feel problem is in type name for product i gave

 <type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider">

working fine
but for order grid i gave

 <type name="sales_order_grid_data_source">

but sales order grid through error

Fatal error: Call to undefined method Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider::addField() in D:\xampp\htdocs\magento2\vendor\magento\module-ui\Component\Listing\Columns\Column.php on line 116

below are the code for ading my custom field to order and product grid

  1. app\code\Sugarcode\Test\etc\adminhtml\di.xml

    <type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider">
        <arguments>
            <argument name="addFieldStrategies" xsi:type="array">
                <item name="title" xsi:type="object">Sugarcode\Test\Ui\DataProvider\Test\AddTitleFieldToCollection</item>
            </argument>
            <argument name="addFilterStrategies" xsi:type="array">
                <item name="title" xsi:type="object">Sugarcode\Test\Ui\DataProvider\Test\AddTitleFilterToCollection</item>
            </argument>
        </arguments>
    </type>
    <type name="sales_order_grid_data_source">
        <arguments>
            <argument name="addFieldStrategies" xsi:type="array">
                <item name="title" xsi:type="object">Sugarcode\Test\Ui\DataProvider\Test\AddTitleFieldToOrderCollection</item>
            </argument>
            <argument name="addFilterStrategies" xsi:type="array">
                <item name="title" xsi:type="object">Sugarcode\Test\Ui\DataProvider\Test\AddTitleFilterToOrderCollection</item>
            </argument>
        </arguments>
    </type>
    

  2. app\code\Sugarcode\Test\view\adminhtml\ui_component\sales_order_grid.xml

    <columns name="sales_order_columns">
        <column name="title">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="add_field" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Titles</item>
                    <item name="sortOrder" xsi:type="number">75</item>
                </item>
            </argument>
        </column>       
    </columns>
    

  3. app\code\Sugarcode\Test\view\adminhtml\ui_component\product_listing.xml

    <columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
        <column name="title">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="add_field" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Titles</item>
                    <item name="sortOrder" xsi:type="number">75</item>
                </item>
            </argument>
        </column>       
    </columns>
    

4 app\code\Sugarcode\Test\Ui\DataProvider\Test\AddTitleFieldToCollection.php

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Sugarcode\Test\Ui\DataProvider\Test;

use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFieldToCollectionInterface;

/**
 * Class AddQuantityFieldToCollection
 */
class AddTitleFieldToCollection implements AddFieldToCollectionInterface
{
    /**
     * {@inheritdoc}
     */
    public function addField(Collection $collection, $field, $alias = null)
    {
        $collection->joinField(
            'title',
            'testtable', //tablename
            'title',  //field
            'id=entity_id', //join condition
            '1', // condition 
            'left'
        );
    }
}

5 app\code\Sugarcode\Test\Ui\DataProvider\Test\AddTitleFieldToOrderCollection.php

 <?php
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Sugarcode\Test\Ui\DataProvider\Test;

    use Magento\Framework\Data\Collection;
    use Magento\Ui\DataProvider\AddFieldToCollectionInterface;

    /**
     * Class AddQuantityFieldToCollection
     */
    class AddTitleFieldToOrderCollection implements AddFieldToCollectionInterface
    {
        /**
         * {@inheritdoc}
         */
        public function addField(Collection $collection, $field, $alias = null)
        {
            $collection->joinField(
                'title',
                'testtable',
                'title',
                'id=entity_id',
                '1',
                'left'
            );
        }
    }

6 app\code\Sugarcode\Test\Ui\DataProvider\Test\AddTitleFilterToCollection.php

<?php
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */

    namespace Sugarcode\Test\Ui\DataProvider\Test;

    use Magento\Eav\Model\Entity\Collection\AbstractCollection;
    use Magento\Framework\Data\Collection;
    use Magento\Ui\DataProvider\AddFilterToCollectionInterface;

    /**
     * Class AddQuantityFilterToCollection
     */
    class AddTitleFilterToCollection implements AddFilterToCollectionInterface
    {
        /**
         * {@inheritdoc}
         */
        public function addFilter(Collection $collection, $field, $condition = null)
        {
            if (isset($condition['like'])) {
                $collection->getSelect()->where(
                    AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX . 'title.title like ?',
                    $condition['like']
                );
            }
        }
    }

7 app\code\Sugarcode\Test\Ui\DataProvider\Test\AddTitleFilterToOrderCollection.php

<?php
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */

    namespace Sugarcode\Test\Ui\DataProvider\Test;

    use Magento\Eav\Model\Entity\Collection\AbstractCollection;
    use Magento\Framework\Data\Collection;
    use Magento\Ui\DataProvider\AddFilterToCollectionInterface;

    /**
     * Class AddQuantityFilterToCollection
     */
    class AddTitleFilterToOrderCollection implements AddFilterToCollectionInterface
    {
        /**
         * {@inheritdoc}
         */
        public function addFilter(Collection $collection, $field, $condition = null)
        {
            if (isset($condition['like'])) {
                $collection->getSelect()->where(
                    AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX . 'title.title like ?',
                    $condition['like']
                );
            }
        }
    }

can any one help me :-)

1

2 Answers 2

1

As I see $addFieldStrategies property is declared only in ProductDataProvider, so it looks to be applicable only for product data providers.

Please check this article for step by step instructions on adding a column to Sales Order Grid.

1
  • Link does not appear to be working anymore.
    – Louis B.
    Commented Feb 17, 2022 at 5:33
0

There are few ways to add a column to Magento's core grids or other grids i.e. sales order.

The possible two ways are

  1. using plugin to join on sales order grid collection with other tables (see my answer here)
  2. create a column in sales_order_grid table and save values in that column for an order. (see Sergey's post)

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.