英文:
Shopware 6.5: Get a SalesChannelProductEntity from Order Line Items?
问题
我使用SalesChannelRepositoryIterator
和SalesChannelProductRepository
来获取产品。
现在我想获取订单并需要遍历它们。我使用RepositoryIterator
与OrderRepository
从数据库中获取订单。在遍历订单项时,我使用$product = $lineItem->getProduct();
但然后我得到的是ProductEntity
而不是我需要的SalesChannelProductEntity
。
如何从订单项获取SalesChannelProductEntity
?或者有没有办法从ProductEntity
迁移到SalesChannelProductEntity
?
英文:
I work with the SalesChannelRepositoryIterator
and SalesChannelProductRepository
when fetching Products.
Now I want to fetch Orders and need to iterate through them. I use the RepositoryIterator
with the OrderRepository
to get the Orders from the Database. When looping through the OrderItems I do $product = $lineItem->getProduct();
But then I get the ProductEntity
and not the SalesChannelProductEntity
which I need.
How can I get the SalesChannelProductEntity
from the OrderItems? Or is there any way to migrate from a ProductEntity
to a SalesChannelProductEntity
?
答案1
得分: 2
最简单的解决方法是在迭代时使用 sales_channel.product.repository
来获取产品。否则,您将不得不为 order
实体实现自己的 SalesChannelDefinitionInterface
。
您需要组装一个 SalesChannelContext
来获取产品实体的销售渠道变体。您可以使用 OrderConverted
服务来执行此操作。
$criteria = new Criteria();
$criteria->addAssociations([
'lineItems',
'transactions',
]);
$iterator = new RepositoryIterator(
$this->orderRepository,
$context,
$criteria
);
foreach ($iterator->fetch()->getEntities() as $order) {
$salesChannelContext = $this->container->get(OrderConverter::class)
->assembleSalesChannelContext($order, $context);
foreach ($order->getLineItems() as $lineItem) {
$product = $this->container->get('sales_channel.product.repository')->search(
new Criteria([$lineItem->getProductId()]),
$salesChannelContext
)->first();
var_dump($product instanceof SalesChannelProductEntity);
}
}
英文:
Easiest solution would be to simply fetch the product while iterating using sales_channel.product.repository
. Otherwise you'd have to implement your own SalesChannelDefinitionInterface
for the order
entity.
You'll have to assemble a SalesChannelContext
to fetch the sales channel variant of the product entity. You can use the OrderConverted
service to do so.
$criteria = new Criteria();
$criteria->addAssociations([
'lineItems',
'transactions',
]);
$iterator = new RepositoryIterator(
$this->orderRepository,
$context,
$criteria
);
foreach ($iterator->fetch()->getEntities() as $order) {
$salesChannelContext = $this->container->get(OrderConverter::class)
->assembleSalesChannelContext($order, $context);
foreach ($order->getLineItems() as $lineItem) {
$product = $this->container->get('sales_channel.product.repository')->search(
new Criteria([$lineItem->getProductId()]),
$salesChannelContext
)->first();
var_dump($product instanceof SalesChannelProductEntity);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论