How to show user attributes in the customer's table in Shopware 5?

Viewed 355

I have multiple custom fields for the user in s_user_attributes. I was wondering if there is a way to show them in the customer's overview view. I know that it is possible to create a different window, but is it possible to do within the default one?

There is such a documentation, but what I search for is missing there: https://developers.shopware.com/developers-guide/backend-components/listing/

1 Answers

Yes, it is possible to extend the listing view. In my example billingPhone and shippingPhone have been added.

First you have to subscribe to the following events:

'Enlight_Controller_Action_PostDispatchSecure_Backend_Customer' => 'onCustomerPostDispatch',
'Shopware_Controllers_Backend_CustomerQuickView::getModelFields::after' => 'addPhoneOnFields',
'Shopware_Controllers_Backend_CustomerQuickView::getListQuery::after' => 'addPhoneOnSelect',

Then have to load the data:

public function addPhoneOnSelect(\Enlight_Hook_HookArgs $args)
    {
        /** @var \Shopware_Controllers_Backend_CustomerQuickView $subject */
        $return = $args->getReturn();
        $return->addSelect('billing.phone as billingPhone');
        $return->addSelect('shipping.phone as shippingPhone');
        $return->leftJoin('customer.defaultShippingAddress', 'shipping');
        $args->setReturn($return);
    }

    public function addPhoneOnFields(\Enlight_Hook_HookArgs $args)
    {
        $return = $args->getReturn();
        $return['billingPhone'] = ['alias' => 'billing.phone', 'type' => 'string'];
        $return['shippingPhone'] = ['alias' => 'shipping.phone', 'type' => 'string'];
        $args->setReturn($return);
    }

Then you have to extend the view with the data

public function onCustomerPostDispatch(Enlight_Controller_ActionEventArgs $args)
    {
        /** @var \Enlight_Controller_Action $controller */
        $controller = $args->getSubject();
        $view = $controller->View();
        $request = $controller->Request();

        $view->addTemplateDir(($this->container->getParameter('reply_extend_customer_overview.plugin_dir') . '/Resources/views/'));

        if ($request->getActionName() == 'load') {
            $view->extendsTemplate('backend/swag_extend_customer_overview/view/detail/window.js');
            $view->extendsTemplate('backend/swag_extend_customer_overview/model/quick_view.js');
            $view->extendsTemplate('backend/swag_extend_customer_overview/view/main/customer_list.js');
        }
    }

You have to extend the model for the listing like this

// {block name="backend/customer/model/quick_view/fields" append}
{ name: 'billingPhone', type: 'string', useNull: true },
{ name: 'shippingPhone', type: 'string', useNull: true },
// {/block}

And the listing

// {namespace name=backend/customer/view/main}
// {block name="backend/customer/view/main/customer_list" append}
Ext.define('Shopware.apps.SwagExtendCustomerOverview.view.main.CustomerList', {
    override: 'Shopware.apps.Customer.view.main.CustomerList',
    alias: 'widget.customer-list',

    configure: function() {
        return {
            displayProgressOnSingleDelete: false,

            /* {if {acl_is_allowed privilege=delete}} */
            deleteButton: true,
            deleteColumn: true,
            /* {else} */
            deleteButton: false,
            deleteColumn: false,
            /* {/if} */

            /* {if {acl_is_allowed privilege=detail}} */
            editColumn: true,
            /* {else} */
            editColumn: false,
            /* {/if} */

            /* {if {acl_is_allowed privilege=update}} */
            addButton: true,
            /* {else} */
            addButton: false,
            /* {/if} */

            columns: {
                active: { header: '{s name="active"}{/s}', width: 50 },
                id: { header: '{s name="id"}{/s}' },
                customerGroup: { header: '{s name="column/customer_group"}{/s}' },
                shop: { header: '{s name="shop"}{/s}' },
                number: { header: '{s name="column/number"}{/s}' },
                email: { header: '{s name="email"}{/s}', renderer: this.mailRenderer, flex: 2 },
                salutation: { header: '{s name="salutation"}{/s}', renderer: this.salutationRenderer },
                title: { header: '{s name="title"}{/s}', width: 70 },
                company: { header: '{s name="company"}{/s}' },
                firstname: { header: '{s name="column/first_name"}{/s}' },
                lastname: { header: '{s name="column/last_name"}{/s}' },
                zipcode: { header: '{s name="zip_code"}{/s}' },
                city: { header: '{s name="city"}{/s}' },
                firstLogin: { header: '{s name="first_login"}{/s}' },
                lastLogin: { header: '{s name="lastLogin"}{/s}' },
                accountMode: { header: '{s name="column/accountMode"}{/s}', renderer: this.accountModeRenderer },
                lockedUntil: { header: '{s name="lockedUntil"}{/s}' },
                birthday: { header: '{s name="birthday"}{/s}' },
                billingPhone: { header: '{s name="default_billing_phone_label" namespace="backend/plugins/xxx/translation"}Billing phone{/s}' },
                shippingPhone: { header: '{s name="default_shipping_phone_label" namespace="backend/plugins/xxx/translation"}Billing phone{/s}' },
            }
        };
    },
});
// {/block}
Related