0

So, I have added a column to sales_order table. It is varchar type and it has some data in it.

I found tutorial:

here

that show what I want to achieve exactly.

Now, I am running Magento 2.2.3. and the exact same tutorial is not working for me. It just shows loading element and says

Attention Something went wrong.

and then there is echoed error: "Something went wrong with processing the default view and we have restored the filter to its original state. "

So, here are my classes and xmls:

Collection.php

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

class Collection extends OriginalCollection
{
    protected function _renderFiltersBefore()
    {
        $joinTable = $this->getTable('sales_order');

        $this->getSelect()->joinLeft(
            ["so" => $joinTable],
            'main_table.entity_id = so.entity_id',
            array('customer_manager')
        );

        parent::_renderFiltersBefore();
    }


}

di.xml

 <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="sales_order_grid_data_source"
                  xsi:type="string">Silk\Customerstaff\Model\ResourceModel\Order\Grid\Collection
            </item>
        </argument>
    </arguments>
</type>

sales_order_grid.xml

   <?xml version="1.0" encoding="UTF-8"?>
<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="customer_manager">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Customer Manager</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                <item name="customer_manager" xsi:type="string">sales_order.customer_manager</item>
                <item name="options" xsi:type="object">Silk\Customerstaff\Ui\Component\Listing\Column\Order\CustomerManagerOptions</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

And CustomerManagerOptions.php

class CustomerManagerOptions
{
    /**
     * @var array
     */
    protected $options;

    /**
     * @var CollectionFactory
     */
    protected $collectionFactory;

    /**
     * Constructor
     *
     * @param CollectionFactory $collectionFactory
     */
    public function __construct(CollectionFactory $collectionFactory)
    {
        $this->collectionFactory = $collectionFactory;
    }

    /**
     * Get options
     *
     * @return array
     */
    public function optionarray()
    {
        \Zend_Debug::dump('levu levic');
        if ($this->options === null) {
            $this->options = $this->collectionFactory->create()->toOptionArray();
        }

        return array(array('value' => 'Vukasin Vulovic', 'label' => 'Vukasin Vulovic'));
    }


}

Does anybody have any idea why is this happening?

Thanks

  • You have a value in sales_order table right?? say it as coupon column – Prathap Gunasekaran Mar 7 at 16:03
  • @PrathapGunasekaran Yes, I have customer_manager column in sales_order table – Vulovic Vukasin Mar 7 at 16:05
  • So you want to fetch that into sales_order_grid and list it in grid view right? – Prathap Gunasekaran Mar 7 at 16:06
  • @PrathapGunasekaran yep, show it in grid and be able to filter it and sort it. – Vulovic Vukasin Mar 7 at 16:07
  • Do you have same column in sales_order_grid table as sales_order table – Prathap Gunasekaran Mar 7 at 16:09
1

Try this,

Add a column in sales_order_grid table like you have in sales_order

Then add a sales_order_grid.xml in the below path

app/code/Vendor/ModuleName/view/adminhtml/ui_component/sales_order_grid.xml

add the below code in it

<?xml version="1.0" encoding="UTF-8"?>                         
<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="customer_manager">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">text</item>
            <item name="label" xsi:type="string" translate="true">Column Name Goes here</item>
            <item name="visible" xsi:type="boolean">false</item>
        </item>
    </argument>
</column>                                                    
</columns>                                                   
</listing>

then add di.xml in the below path

app/code/Vendor/ModuleName/etc/di.xml

add the below code in it

<?xml version="1.0"?>                                                   
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
<arguments>
    <argument name="columns">
        <item name="customer_manager" xsi:type="string">sales_order.customer_manager</item>
    </argument>
</arguments>                                                     
</virtualType>                                                        
</config>

NOTE : When you post data into sales_order table next time then it will also be added into sales_order_grid table as then xml will list the column in grid view and you can add filter and sortable option to it.

Run below commands after you made these changes

php bin/magento setup:upgrade
php bin/magento cache:flush

Hope this helps :)

  • 1
    Yes, it works, great! Now the thing is is that I want to filter it via select, how to achieve that? – Vulovic Vukasin Mar 7 at 19:23
  • If it helps you means, do accept it so others will get benefit and what do you mean by filter via select.Can you elaborate a bit? – Prathap Gunasekaran Mar 8 at 4:13
  • I will when you answer my question to the end. So, I need to have a filter, not text type but select, where it would show me all the customer_manager in a dropdown. – Vulovic Vukasin Mar 8 at 7:18
  • after argument put this <item name="options" xsi:type="object">Namespace\Modulename\Model\Config\Source\File</item> then make it as select then load the collection of array whatever you want to display it as option array. – Prathap Gunasekaran Mar 8 at 7:43
  • This file Namespace\Modulename\Model\Config\Source\File.php should have the optionarray() to list it in the select dropdown and it may be static or dynamic up to your requirement. thanks – Prathap Gunasekaran Mar 8 at 7:44
0

Modify your sales_order_grid.xml, add the options and componentin column:

<?xml version="1.0" encoding="UTF-8"?>
<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="customer_manager">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Silk\Customerstaff\Model\Config\Source\CustomerManager</item>
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">select</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Customer Manager</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                <item name="customer_manager" xsi:type="string">sales_order.customer_manager</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>  

EDIT Add this file to this Path

Silk\Customerstaff\Model\Config\Source\CustomerManager;

CustomerManager.php

<?php

namespace Silk\Customerstaff\Model\Config\Source;

class CustomerManager implements \Magento\Framework\Option\ArrayInterface
{
/**
 * Options getter
 *
 * @return array
 *
 */
public function toOptionArray()
{
    return [
        ['value' => 0, 'label' => __('Option 1')],
        ['value' => 1, 'label' => __('Option 2')]
    ];
}
}
  • This is what I get if I use your code: Exception #0 (Exception): Warning: array_values() expects parameter 1 to be array, object given in /home/magecomm/rtam/vendor/magento/module-ui/Component/Form/Element/AbstractOptionsField.php on line 59 – Vulovic Vukasin Mar 11 at 9:17
  • edit your CustomerManagerOptions return like this return array('value' => 'Vukasin Vulovic', 'label' => 'Vukasin Vulovic'); – fmsthird Mar 11 at 9:28
  • Same thing happens... – Vulovic Vukasin Mar 11 at 9:35
  • which collectionFactory you are using in your customerManager? – fmsthird Mar 11 at 9:39
  • It doesn't matter, even when I remove the constructor, it still throws the same error – Vulovic Vukasin Mar 11 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.