How to show Customer Group on grid on Magento

 

How to show Customer Group on grid on Magento

Hi Magento friend today's i'm share How to show Customer Group on grid on Magento 


<column name="customer_group" class="Mageget\WebPushNotification\Ui\Component\Listing\Grid\Column\CustomerGroup">

    <argument name="data" xsi:type="array">

        <item name="config" xsi:type="array">

            <item name="filter" xsi:type="string">true</item>

            <item name="label" xsi:type="string" translate="true">Customer Group</item>

            <item name="sortOrder" xsi:type="number">30</item>

        </item>

    </argument>

</column>


Create a CustomerGroup.php file in - app\code\Mageget\WebPushNotification\Ui\Component\Listing\Grid\Column


<?php

namespace Mageget\WebPushNotification\Ui\Component\Listing\Grid\Column;

use Magento\Framework\UrlInterface;

use Magento\Framework\View\Element\UiComponentFactory;

use Magento\Framework\View\Element\UiComponent\ContextInterface;

use Magento\Ui\Component\Listing\Columns\Column;

use Magento\Customer\Model\ResourceModel\Group\Collection;

/**

 * Class CustomerGroup

*/

 class CustomerGroup extends Column

 {

    private $urlBuilder;

    protected $helper;

    protected $_customerGroup;


    public function __construct(

       ContextInterface $context,

       UiComponentFactory $uiComponentFactory,

       UrlInterface $urlBuilder,

       Collection $customerGroup,

       array $components = [],

       array $data = []

    ) {

       parent::__construct($context, $uiComponentFactory, 

       $components, $data

    );

       $this->urlBuilder = $urlBuilder;

       $this->_customerGroup = $customerGroup; 

    }

  public function prepareDataSource(array $dataSource)

   {

    if (isset($dataSource['data']['items'])) {

        $fieldName = 'customer_group';

        foreach ($dataSource['data']['items'] as & $item) {

            if (!empty($item['customer_group'])) {

                $customer_group_id = $item['customer_group'];

                $customer_group_id = explode(",",$customer_group_id);

                $customerGroups = $this->_customerGroup->toOptionArray();

                $arrData = array();

                foreach($customerGroups as $cg){

                    if(in_array($cg['value'],$customer_group_id)){

                        $arrData []= $cg['label'];

                    }

                }

                $customer_lable = implode(" | ",$arrData);

                $item[$fieldName] = $customer_lable;

            }

        }

    }

    return $dataSource;

  }


How to show Customer Group on grid on Magento




 

Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post