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
相关产品推荐
相关产品推荐

