0

I have a custom module created to show list of backorder product, the list appear like this

enter image description here

as you can see on the grid table above under the column payment status there is 2 type of status which is

  • payment_receive
  • pending_payment

enter image description here

I don't want the list to appear all payment_status list, instead I want it to appear only payment_receive, means the query that call the list for this table if dumped in sql will be like

SELECT * FROM gshop_backorders WHERE payment_status = 'payment_receive';

then I want to hide the payment_status column

I have check for many ways such as adding getData function inside the DataProvider.php but unable to figure out how to do this, below is the code at app/code/GShop/Order/Model/Gshopbackorders/DataProvider.php

declare(strict_types=1);

namespace GShop\Order\Model\Gshopbackorders;

use GShop\Order\Model\ResourceModel\GshopBackorders\CollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Ui\DataProvider\AbstractDataProvider;

class DataProvider extends AbstractDataProvider
{

    /**
     * @inheritDoc
     */
    protected $collection;

    /**
     * @var DataPersistorInterface
     */
    protected $dataPersistor;

    /**
     * @var array
     */
    protected $loadedData;

    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $collectionFactory
     * @param DataPersistorInterface $dataPersistor
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        DataPersistorInterface $dataPersistor,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $collectionFactory->create();
        $this->dataPersistor = $dataPersistor;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    /**
     * @inheritDoc
     */
    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();
        foreach ($items as $model) {
            $this->loadedData[$model->getId()] = $model->getData();
        }
        $data = $this->dataPersistor->get('gshop_backorders');
        
        if (!empty($data)) {
            $model = $this->collection->getNewEmptyItem();
            $model->setData($data);
            $this->loadedData[$model->getId()] = $model->getData();
            $this->dataPersistor->clear('gshop_backorders');
        }
        
        return $this->loadedData;
    }
}

and the ui component

app/code/GShop/Order/view/adminhtml/ui_component/gshop_order_gshop_backorders_listing.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">gshop_order_gshop_backorders_listing.gshop_order_gshop_backorders_listing_data_source</item>
        </item>
    </argument>
    <settings>
        <spinner>gshop_order_gshop_backorders_columns</spinner>
        <deps>
            <dep>gshop_order_gshop_backorders_listing.gshop_order_gshop_backorders_listing_data_source</dep>
        </deps>
    </settings>
    <dataSource name="gshop_order_gshop_backorders_listing_data_source" component="Magento_Ui/js/grid/provider">
        <settings>
            <storageConfig>
                <param name="indexField" xsi:type="string">backorders_id</param>
            </storageConfig>
            <updateUrl path="mui/index/render"/>
        </settings>
        <aclResource>GShop_Order::gshop_backorders</aclResource>
        <dataProvider name="gshop_order_gshop_backorders_listing_data_source" class="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
            <settings>
                <requestFieldName>id</requestFieldName>
                <primaryFieldName>backorders_id</primaryFieldName>
            </settings>
        </dataProvider>
    </dataSource>
    <massaction name="listing_massaction">
        <action name="download">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">download</item>
                    <item name="label" xsi:type="string" translate="true">Download</item>
                    <item name="url" xsi:type="url" path="gshop_order/export/massdownload"/>
                </item>
            </argument>
        </action>
        <action name="placeorder">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">placeorder</item>
                    <item name="label" xsi:type="string" translate="true">Place Order</item>
                    <item name="url" xsi:type="url" path="gshop_order/placeorder/index"/>
                </item>
            </argument>
        </action>
    </massaction>
    <listingToolbar name="listing_top">
        <settings>
            <sticky>true</sticky>
        </settings>
        <bookmark name="bookmarks"/>
        <columnsControls name="columns_controls"/>
        <filters name="listing_filters"/>
        <paging name="listing_paging"/>
    </listingToolbar>
    <columns name="gshop_order_gshop_backorders_columns">
        <settings>
            <editorConfig>
                <param name="selectProvider" xsi:type="string">gshop_order_gshop_backorders_listing.gshop_order_gshop_backorders_listing.gshop_order_gshop_backorders_columns.ids</param>
                <param name="enabled" xsi:type="boolean">true</param>
                <param name="indexField" xsi:type="string">backorders_id</param>
                <param name="clientConfig" xsi:type="array">
                    <item name="saveUrl" xsi:type="url" path="gshop_order/gshopbackorders/inlineEdit"/>
                    <item name="validateBeforeSave" xsi:type="boolean">false</item>
                </param>
            </editorConfig>
            <childDefaults>
                <param name="fieldAction" xsi:type="array">
                    <item name="provider" xsi:type="string">gshop_order_gshop_backorders_listing.gshop_order_gshop_backorders_listing.gshop_order_gshop_backorders_columns_editor</item>
                    <item name="target" xsi:type="string">startEdit</item>
                    <item name="params" xsi:type="array">
                        <item name="0" xsi:type="string">${ $.$data.rowIndex }</item>
                        <item name="1" xsi:type="boolean">true</item>
                    </item>
                </param>
            </childDefaults>
        </settings>

        <selectionsColumn name="ids">
            <settings>
                <indexField>backorders_id</indexField>
            </settings>
        </selectionsColumn>
        <column name="backorders_id">
            <settings>
                <filter>text</filter>
                <sorting>asc</sorting>
                <label translate="true">ID</label>
            </settings>
        </column>
        <column name="backorders_id">
            <settings>
                <filter>text</filter>
                <label translate="true">ID</label>
            </settings>
        </column>
        <column name="product_name">
            <settings>
                <filter>text</filter>
                <label translate="true">Product Name</label>
            </settings>
        </column>
        <column name="sku">
            <settings>
                <filter>text</filter>
                <label translate="true">SKU</label>
            </settings>
        </column>
        <column name="order_entity_id">
            <settings>
                <filter>text</filter>
                <label translate="true">Order Entity Id</label>
            </settings>
        </column>
        <column name="pre_order_number">
            <settings>
                <filter>text</filter>
                <label translate="true">Pre Order Number</label>
            </settings>
        </column>
        <column name="gtrust_orderno">
            <settings>
                <filter>text</filter>
                <label translate="true">Gtrust Order Number</label>
            </settings>
        </column>
        <column name="payment_method">
            <settings>
                <filter>text</filter>
                <label translate="true">Payment Method</label>
            </settings>
        </column>
        <column name="payment_status">
            <settings>
                <filter>text</filter>
                <label translate="true">Payment Status</label>
            </settings>
        </column>
        <column name="payment_transaction_id">
            <settings>
                <filter>text</filter>
                <label translate="true">Payment Transaction Id</label>
            </settings>
        </column>
        <column name="customer_id">
            <settings>
                <filter>text</filter>
                <label translate="true">Customer Id</label>
            </settings>
        </column>
        <column name="customer_email">
            <settings>
                <filter>text</filter>
                <label translate="true">Customer Email</label>
            </settings>
        </column>
        <column name="qty">
            <settings>
                <filter>text</filter>
                <label translate="true">Qty</label>
            </settings>
        </column>
        <column name="created_at">
            <settings>
                <filter>text</filter>
                <label translate="true">Created At</label>
            </settings>
        </column> 
        <column name="order_datetime">
            <settings>
                <filter>text</filter>
                <label translate="true">Order Date Time</label>
            </settings>
        </column> 
        <actionsColumn name="actions" class="GShop\Order\Ui\Component\Listing\Column\GshopbackordersActions">
            <settings>
                <indexField>backorders_id</indexField>
                <resizeEnabled>false</resizeEnabled>
                <resizeDefaultWidth>107</resizeDefaultWidth>
            </settings>
        </actionsColumn>
    </columns>
</listing>

Appreciate someone that can help explain what is the ways to add in this filter so that only payment_status = 'payment_receive' data appear in the list

1 Answer 1

0

Finally able to find out solution to this problem after a lots of try and error and dumping data to understand

Basically just need to add this code into the __construct function of the DataProvider

$filter = $this->filterBuilder
->setConditionType('like')
->setField('payment_status')
->setValue('%payment_receive%')
->create();

$this->addFilter($filter);

thus the finalized code for app/code/GShop/Order/Model/Gshopbackorders/DataProvider.php is as below

declare(strict_types=1);

namespace GShop\Order\Model\Gshopbackorders;

use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
use Magento\Framework\Api\Search\ReportingInterface;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\SearchResultInterface;

class BackorderDataProvider extends DataProvider
{

    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $collectionFactory
     * @param DataPersistorInterface $dataPersistor
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        ReportingInterface $reporting,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        RequestInterface $request,
        FilterBuilder $filterBuilder,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $reporting, $searchCriteriaBuilder, $request, $filterBuilder, $meta, $data);
        
        $filter = $this->filterBuilder
        ->setConditionType('like')
        ->setField('payment_status')
        ->setValue('%payment_receive%')
        ->create();

        $this->addFilter($filter);
    }

}

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.