Magento 2 $block->getCustomer() return NULL

64 views Asked by At

I want to get customer data from $customer = $block->getCustomer(); after users sign up, Magento will send a custom confirmation email. However, the $customer return is null. Please let me know what is wrong. Thank you!

magento/app/code/ABC/Customer/view/frontend/layout/account_new_confirmation.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Confirmation" design_abstraction="custom">
    <body>
        <block class="Magento\Framework\View\Element\Template" name="account.new.confirmation" template="Customer::email/account_new_confirmation.phtml"/>
    </body>
</page>

magento/app/code/ABC/Customer/view/frontend/templates/email/account_new_confirmation.phtml

<?php
/** @var Magento\Framework\View\Element\Template $block */
$customer = $block->getCustomer();
?>
1

There are 1 answers

0
iranimij On

I just write an example of using getCustomer function and then you can compare the differences, How magento use this block?

In magento/module-customer/view/frontend/layout/customer_account_edit.xml

There is below code :

<referenceContainer name="content">
    <block class="Magento\Customer\Block\Form\Edit" name="customer_edit" template="Magento_Customer::form/edit.phtml" cacheable="false">
        <container name="form.additional.info" as="form_additional_info"/>
    </block>
</referenceContainer>

and then in magento/module-customer/view/frontend/templates/form/edit.phtml, we have something like below code, Line 21 :

/** @var \Magento\Customer\Block\Form\Edit $block */
<?= $block->getLayout()->createBlock(Name::class)->setObject($block->getCustomer())->toHtml() ?>

So If you inspect vendor/magento/module-customer/Block/Form/Edit.php you will notice that this block parent has getCustomer function but your block and its parents do not.