0

I have extension installed in my project which converts guest customer to customer, and i want to display this customer is "Guest to Customer" in my custom column in customer grid, enter image description here Something like that in this picture. Thanks in advance.

2

1 Answer 1

1

1. Register column with custom column class

app/code/Acme/StackExchange/view/adminhtml/ui_component/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="convert_to_customer" class="Acme\StackExchange\Ui\Component\Listing\Column\ConvertToCustomer" sortOrder="90">
            <settings>
                <label translate="true">Convert to Customer</label>
                <bodyTmpl>ui/grid/cells/html</bodyTmpl>
                <filter>false</filter>
                <sortable>false</sortable>
            </settings>
        </column>
    </columns>
</listing>

2. Create Column processor

app/code/Acme/StackExchange/Ui/Component/Listing/Column/ConvertToCustomer.php

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Ui\Component\Listing\Column;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class ConvertToCustomer extends Column
{
    protected UrlInterface $urlBuilder;

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = []
    ) {
        $this->urlBuilder = $urlBuilder;

        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * @inheritDoc
     */
    public function prepareDataSource(array $dataSource): array
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {
                $content = '&nbsp;';
                if (empty($item['customer_id'])) {
                    $url     = $this->urlBuilder->getUrl('path/to/url', [
                        'order_id' => $item['entity_id'],
                    ]);
                    $title   = __('Guest To Customer');
                    $content = <<<HTML
<a target="_blank" class="product_link" href="{$url}">{$title}</a>
HTML;
                }
                $item[$this->getName()] = $content;
            }
        }

        return $dataSource;
    }
}
3

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.