I am trying to add Shipping country as extra column and filter,
how that can be achieved. can anyone help me on this.
referred the below Url, but which was not helpful for me. Magento 2: How to add custom filters in Orders Report?
How to add new field in the Sales order report with the shipping country drop down list filter.
Vendor/Module/etc/adminhtml/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">
<preference for="Magento\Sales\Block\Adminhtml\Report\Filter\Form\Order" type="Vendor\Module\Block\Adminhtml\Report\Filter\Form\Order" />
<preference for="Magento\Reports\Block\Adminhtml\Sales\Sales\Grid" type="Vendor\Module\Block\Adminhtml\Sales\Sales\Grid" />
</config>
Vendor\Module\Block/Adminhtml/Report/Filter/Form/Order.php
class Order extends \Magento\Sales\Block\Adminhtml\Report\Filter\Form\Order
{
protected function _prepareForm()
{
parent::_prepareForm();
$fieldset = $this->getForm()->getElement('base_fieldset');
if (is_object($fieldset) && $fieldset instanceof \Magento\Framework\Data\Form\Element\Fieldset) {
$fieldset->addField(
'shipping_country',
'select',
[
'name' => 'shipping_country',
'options' => ['gbr' => __('United Kingdom'), 'in' => __('India')],
'label' => __('Shipping Country')
]
);
}
return $this;
}
}
Vendor/Module/Block/Adminhtml/Sales/Sales/Grid.php
use Magento\Framework\DataObject;
use Magento\Reports\Block\Adminhtml\Grid\Column\Renderer\Currency;
use Magento\Framework\App\ObjectManager;
use Magento\Sales\Model\Order\ConfigFactory;
use Magento\Sales\Model\Order;
class Grid extends \Magento\Reports\Block\Adminhtml\Sales\Sales\Grid
{
protected $_columnGroupBy = 'period';
private $configFactory;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Backend\Helper\Data $backendHelper,
\Magento\Reports\Model\ResourceModel\Report\Collection\Factory $resourceFactory,
\Magento\Reports\Model\Grouped\CollectionFactory $collectionFactory,
\Magento\Reports\Helper\Data $reportsData,
array $data = [],
ConfigFactory $configFactory = null
) {
parent::__construct(
$context,
$backendHelper,
$resourceFactory,
$collectionFactory,
$reportsData,
$data
);
$this->configFactory = $configFactory ?: ObjectManager::getInstance()->get(ConfigFactory::class);
}
protected function _prepareColumns()
{
$this->addColumn(
'shipping_country',
[
'header' => __('Shipping Country'),
'index' => 'shipping_country',
'type' => 'number',
'sortable' => false,
'header_css_class' => 'col-sales-items',
'column_css_class' => 'col-sales-items'
]
);
return parent::_prepareColumns();
}
}
I have used the above code, a column shown in the grid with the filter.
How to get shipping country in report? I am getting empty values for the column.
please someone update me how this can be achieved?
Thanks in Advance!!