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

Shopware 6 商品对比:导出变体时如何加载父商品productNumber

Shopware 6 获取变体商品父商品productNumber问题

你在导出商品变体时尝试用{{ product.parent.productNumber }}获取父商品的productNumber,收到报错提示:

"It is not possible to read the parent association directly. Please read the parents via a separate call over the repository"

直接用product.parentId只能拿到父商品的ID值,没法直接获取productNumber这类属性——parentId只是个外键,并没有关联加载父商品的完整数据。

要获取父商品的productNumber,得通过ProductRepository单独查询:

  • 后端服务中可以这样实现:
$parentProduct = $this->productRepository->search(
    (new Criteria())->addFilter(new EqualsFilter('id', $product->getParentId())),
    $context
)->first();
$parentProductNumber = $parentProduct ? $parentProduct->getProductNumber() : null;
  • 如果是在Twig模板里,建议提前在后端逻辑中把父商品数据加载好再传递到模板里使用,避免在模板里直接做仓库查询影响性能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 08:21:35