You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2注册确认邮件中$block->getCustomer()返回NULL问题排查

Magento注册确认邮件中$block->getCustomer()返回NULL的排查与解决

问题原因

  1. 基础模板块无getCustomer()方法
    你使用的Magento\Framework\View\Element\Template是最基础的模板块,本身没有内置getCustomer()方法,也不会自动关联客户对象。调用该方法时,由于魔术方法找不到对应属性,直接返回NULL。

  2. 未向块传递客户数据
    邮件渲染流程脱离当前请求上下文,无法通过会话自动获取客户。如果没有在邮件发送时显式将客户数据传递给块实例,块自然无法获取到有效数据。

解决方案

方案一:自定义块类并注入客户逻辑

  1. 创建自定义块类,继承基础模板块并添加客户获取逻辑:
// magento/app/code/ABC/Customer/Block/Email/AccountNewConfirmation.php
namespace ABC\Customer\Block\Email;

use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\Customer;

class AccountNewConfirmation extends Template
{
    protected $customerModel;

    public function __construct(
        Template\Context $context,
        Customer $customerModel,
        array $data = []
    ) {
        $this->customerModel = $customerModel;
        parent::__construct($context, $data);
    }

    public function getCustomer()
    {
        // 通过传递的客户ID加载客户对象
        if ($customerId = $this->getData('customer_id')) {
            return $this->customerModel->load($customerId);
        }
        return null;
    }
}
  1. 修改布局文件,替换块类为自定义类:
<?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="ABC\Customer\Block\Email\AccountNewConfirmation" name="account.new.confirmation" template="Customer::email/account_new_confirmation.phtml"/>
    </body>
</page>
  1. 发送邮件时传递客户ID到块:
    在触发邮件发送的代码中,将客户ID传入块的数据集:
$transport = $this->transportBuilder
    ->setTemplateIdentifier('your_email_template_id')
    ->setTemplateOptions([
        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
        'store' => $storeId
    ])
    ->setTemplateVars([
        'block' => $this->layoutFactory->create()
            ->createBlock(\ABC\Customer\Block\Email\AccountNewConfirmation::class)
            ->setData('customer_id', $customer->getId())
    ])
    ->setFrom($sender)
    ->addTo($customer->getEmail())
    ->getTransport();
$transport->sendMessage();

方案二:直接传递客户对象到模板变量

跳过块的方法调用,直接在邮件发送时将客户对象作为模板变量传递:

  1. 发送邮件时添加customer变量:
$transport = $this->transportBuilder
    ->setTemplateIdentifier('your_email_template_id')
    ->setTemplateOptions([
        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
        'store' => $storeId
    ])
    ->setTemplateVars([
        'customer' => $customer,
        'block' => $this->layoutFactory->create()
            ->createBlock(\Magento\Framework\View\Element\Template::class)
            ->setTemplate('ABC_Customer::email/account_new_confirmation.phtml')
    ])
    ->setFrom($sender)
    ->addTo($customer->getEmail())
    ->getTransport();
$transport->sendMessage();
  1. 在模板中直接使用客户对象:
<?php
/** @var Magento\Framework\View\Element\Template $block */
/** @var \Magento\Customer\Model\Customer $customer */
$customer = $block->getData('customer');
// 直接使用$customer的属性,比如$customer->getName()
?>

注意事项

  • 邮件渲染脱离请求上下文,不能依赖客户会话获取数据,必须显式传递客户ID或对象。
  • 修改代码后,执行bin/magento setup:upgrade、bin/magento setup:di:compile命令,确保自定义块类生效。

内容的提问来源于stack exchange,提问作者Tri Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 13:05:34