Вопрос

I want to show the Company Name in Header.phtml but its working as expected. Can anyone please guide me how to display the company name in header section?

Это было полезно?

Решение

I have found a way for it. We can achieve this with the use of plugin method. Please follow below steps.

Step 1: Create di.xml file under path PackageName/Module/etc/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\CustomerData\Customer">
        <plugin name="additional_section_data" type="PackageName\Module\Plugin\CustomerData\Customer" />
    </type>
</config>

Step 2: Create Customer.php file under path PackageName/Module/Plugin/CustomerData/

<?php

namespace PackageName\Module\Plugin\CustomerData;

use Magento\Customer\Helper\Session\CurrentCustomer;

class Customer
{
    /**
     * @var CurrentCustomer
     */
    private $currentCustomer;
    protected $accountManagement;

    public function __construct(
        CurrentCustomer $currentCustomer,
        \Magento\Customer\Api\AccountManagementInterface $accountManagement
    ) {
        $this->currentCustomer = $currentCustomer;
        $this->accountManagement = $accountManagement;
    }

    public function afterGetSectionData(\Magento\Customer\CustomerData\Customer $subject, $result)
    {
        if ($this->currentCustomer->getCustomerId()) {
            $customer = $this->currentCustomer->getCustomer();
            $result['company'] = $this->getDefaultShippingAddress($this->currentCustomer->getCustomerId())->getCompany();
        }

        return $result;
    }

    public function getDefaultShippingAddress($customerId)
    {
        try {
            $address = $this->accountManagement->getDefaultShippingAddress($customerId);
        } catch (NoSuchEntityException $e) {
            return __('You have not added default shipping address. Please add default shipping address.');
        }
        return $address;
    }
}

In above code i have got "company" from Default Shipping Address. Please change according to your requirement.

Now, to check everything is working fine till here, you can check local storage of your browser. If you are using chrome then you can see it under Application tab as in screenshot below.

https://prnt.sc/103fz7f

Step 3: You need "company" name under the welcome message so i have overridden below template file according to your need.

Create default.xml file under path PackageName/Module/view/frontend/layout/

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance dc" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="customer">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">PackageName_Module::account/customer.phtml</argument>
        </action>
    </referenceBlock>
</page>

Step 4: Create customer.phtml file under path PackageName/Module/view/frontend/templates/account/

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var Magento\Customer\Block\Account\Customer $block */
?>
<?php if ($block->customerLoggedIn()) : ?>
    <li class="customer-welcome">
        <span class="customer-name"
              role="link"
              tabindex="0"
              data-mage-init='{"dropdown":{}}'
              data-toggle="dropdown"
              data-trigger-keypress-button="true"
              data-bind="scope: 'customer'">
            <button type="button"
                    class="action switch"
                    tabindex="-1"
                    data-action="customer-menu-toggle">
                <span><?= $block->escapeHtml(__('Change')) ?></span>
            </button>
        </span>
        <script type="text/x-magento-init">
        {
            "*": {
                "Magento_Ui/js/core/app": {
                    "components": {
                        "customer": {
                            "component": "Magento_Customer/js/view/customer"
                        }
                    }
                }
            }
        }
        </script>
        <?php if ($block->getChildHtml()) :?>
            <div class="customer-menu test" data-target="dropdown" data-bind="scope: 'customer'">
                <span data-bind="text: customer().company"></span>
                <?= $block->getChildHtml() ?>
            </div>
        <?php endif; ?>
    </li>
<?php endif; ?>

In above code i have used span tag to show company name like this

<span data-bind="text: customer().company"></span>

Please check and let me know if you have any query. This will surely work for you.

Thanks! Cheers

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top