3

Want to save column data

instore_order_number (present in both tables)

from sales_order to sales_order_grid

Currntly I am able to save the instore_order_number from sales_order to sales_order_grid but facing issue in mapping i.e. when I am saving instore_order_number it is creating a new row with 0 id i.e. it is not mapping the sales_order with sales_order_grid

Code: di.xml

<virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid">
        <arguments>
            <argument name="joins" xsi:type="array">
                <item name="sales_order_grid" xsi:type="array">
                    <item name="table" xsi:type="string">sales_order_grid</item>
                    <item name="origin_column" xsi:type="string">entity_id</item>
                    <item name="target_column" xsi:type="string">entity_id</item>
                </item>
            </argument>
            <argument name="columns" xsi:type="array">
                <item name="instore_order_number" xsi:type="string">sales_order.instore_order_number</item>
            </argument>
        </arguments>
    </virtualType>

sales_order_grid.xml

<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="instore_order_number">
            <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">instore_order_number</item>
                </item>
            </argument>
        </column>
    </columns>

Note: didn't made install schema as the attribute was present in both tables

1 Answer 1

-1

In di.xml :

<?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" xsi:type="array">
          <item name="instore_order_number" xsi:type="string">sales_order.instore_order_number</item>
        </argument>
    </arguments>
</virtualType>
</config>

In Package/Module/view/adminhtml/ui_component under 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="instore_order_number" class="Package\Module\Ui\Component\moduleGridColoumn\OrderNumber">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Instore order number</item>
                <item name="sortOrder" xsi:type="number">99</item>
            </item>
        </argument>
    </column>
</columns>
</listing>

In Package/Module/Ui/Component/moduleGridColoumn under OrderNumber.php

<?php
namespace Package\Module\Ui\Component\moduleGridColoumn;
use Magento\Framework\Escaper;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory;
class OrderNumber extends Column {

/**
 *
 * @var Object 
 */
protected $_resource;

/**
 *
 * @var Object 
 */
protected $_scopeConfig;

/**
 *
 * @var Object 
 */
protected $escaper;

public function __construct(
ContextInterface $context, UiComponentFactory $uiComponentFactory, Escaper $escaper, array $components = [], array $data = []
) {
    $this->escaper = $escaper;
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

/**
 * Prepare Data Source
 *
 * @param array $dataSource
 * @return array
 */
public function prepareDataSource(array $dataSource = []) {
    if (isset($dataSource['data']['items'])) {
        foreach ($dataSource['data']['items'] as & $item) {
            if (array_key_exists($this->getData('name'), $item)) {

                $item[$this->getData('name')] = $item['instore_order_number'];
            } else {

                $item[$this->getData('name')] = "";
            }
        }
    }

    return $dataSource;
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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