从订单行项目中获取 SalesChannelProductEntity 在 Shopware 6.5 中。

huangapple go评论62阅读模式
英文:

Shopware 6.5: Get a SalesChannelProductEntity from Order Line Items?

问题

我使用SalesChannelRepositoryIteratorSalesChannelProductRepository来获取产品。

现在我想获取订单并需要遍历它们。我使用RepositoryIteratorOrderRepository从数据库中获取订单。在遍历订单项时,我使用$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);
    }
}

huangapple
  • 本文由 发表于 2023年5月17日 16:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定