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

Shopware 6.4升级至6.5.3.3后订单确认邮件产品缩略图缺失

Shopware 6.5.3.3订单确认邮件产品缩略图不显示解决思路

核心问题定位

升级后订单邮件模板中nestedItem.cover变量未赋值,原因是Shopware 6.5调整了订单行项目的关联数据加载逻辑,默认nestedLineItems不再携带产品封面媒体数据。


具体解决思路

1. 调整模板变量获取路径

尝试通过产品关联直接获取封面,修改邮件模板中的图片渲染代码:

<td>
    {% if nestedItem.product is defined and nestedItem.product.cover is not null and nestedItem.product.cover.media is not null %}
        <img src="{{ nestedItem.product.cover.media.url }}" width="75" height="auto"/>
    {% endif %}
</td>

注意:需确保订单行项目已关联product数据,若仍无法获取,需通过扩展补充数据。

2. 通过事件订阅补充产品封面数据

创建自定义扩展,在邮件发送前为订单行项目加载产品及封面媒体:

// src/Subscriber/OrderMailSubscriber.php
<?php
namespace YourPlugin\Subscriber;

use Shopware\Core\Content\Mail\Service\Event\MailBeforeSentEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderMailSubscriber implements EventSubscriberInterface
{
    private EntityRepository $productRepository;

    public function __construct(EntityRepository $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            MailBeforeSentEvent::class => 'onMailBeforeSent',
        ];
    }

    public function onMailBeforeSent(MailBeforeSentEvent $event): void
    {
        $data = $event->getData();
        if (!isset($data['order'])) {
            return;
        }

        $order = $data['order'];
        $lineItems = $order->getLineItems();
        if (!$lineItems) {
            return;
        }

        // 收集需加载的产品ID
        $productIds = array_filter(
            array_map(fn($item) => $item->getProductId(), $lineItems->getElements()),
            fn($id) => $id !== null
        );

        if (empty($productIds)) {
            return;
        }

        // 加载产品及封面媒体关联
        $products = $this->productRepository->search(
            (new \Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria($productIds))
                ->addAssociation('cover.media'),
            $event->getContext()
        );

        // 为行项目补充产品扩展数据
        foreach ($lineItems as $item) {
            if ($product = $products->get($item->getProductId())) {
                $item->setExtension('productWithCover', $product);
            }
        }
    }
}

之后在模板中通过扩展字段获取:

<td>
    {% if nestedItem.extensions.productWithCover is defined and nestedItem.extensions.productWithCover.cover is not null %}
        <img src="{{ nestedItem.extensions.productWithCover.cover.media.url }}" width="75" height="auto"/>
    {% endif %}
</td>

3. 检查官方模板变更

对比Shopware 6.5官方默认订单确认邮件模板,确认是否有新的变量用法(如lineItem.media等),直接复用官方标准逻辑。

4. 清理缓存确保生效

执行以下命令清除缓存并编译主题:

bin/console cache:clear
bin/console theme:compile

内容的提问来源于stack exchange,提问作者G 111

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 07:00:19