简体   繁体   English

Shopware 6 插件 - 通过 id 加载和显示 ProductStream

[英]Shopware 6 Plugin - Load and display ProductStream via id

I want to load ProductStream data via a given id and display it in the frontend, for example below the product cart.我想通过给定的 id 加载 ProductStream 数据并将其显示在前端,例如在产品购物车下方。 The ProductStream is not linked to a ProductCrossSelling and therefore not linked to a product. ProductStream 未链接到 ProductCrossSelling,因此未链接到产品。

I loaded a ProductStream Collection via the repository.我通过存储库加载了一个 ProductStream 集合。

$criteria = new Criteria($producStreamIds);
$productStreamResult = $this->productStreamRepository->search($criteria, $context);
$productStreams = $productStreamResult->getEntities();

Now I need the associated list of products, to include them via cms-element-product-slider.html.twig .现在我需要相关的产品列表,通过cms-element-product-slider.html.twig包含它们。

I found a post with a similar question: How to get products from the product stream ID in Shopware 6?我找到了一个有类似问题的帖子: How to get products from the product stream ID in Shopware 6?

The answer was, to use the function loadByStream of Core\Content\Product\SalesChannel\CrossSelling\ProductCrossSellingRoute.php , but this function is private.答案是,使用Core\Content\Product\SalesChannel\CrossSelling\ProductCrossSellingRoute.php的 function loadByStream ,但这个 function 是私有的。

The only public function is load and it needs a $productId as a parameter, which I do not have, because my ProductStream is not linked to a product.唯一的公共 function 是load ,它需要一个$productId作为参数,我没有,因为我的 ProductStream 没有链接到产品。

Is there a clean way to load products of a ProductStream that is not linked to a ProductCrossSelling?是否有一种干净的方法来加载未链接到 ProductCrossSelling 的 ProductStream 的产品?

I have currently copied the code of the loadByStream function to load the products for the stream.我目前已经复制了loadByStream function 的代码来加载 stream 的产品。

Or is there an other function in the shopware core that I can use for this case.或者我可以在这种情况下使用 shopware 核心中的其他 function。 I haven't found anything else.我还没有找到其他东西。

It feels as if the current assumption is that ProductStreams are not used without connection to a ProductCrossSelling and thus to a product.感觉好像当前的假设是 ProductStreams 在没有连接到 ProductCrossSelling 并因此连接到产品的情况下不会被使用。

A product stream is really just a collection of criterium filters.产品 stream 实际上只是标准过滤器的集合。 You inject ProductStreamBuilder and call buildFilters with the id of your product stream to retrieve the filters.您注入ProductStreamBuilder并使用产品 ID stream 调用buildFilters以检索过滤器。 Then you use the filters as normal with your criteria, add a limit, sorting etc and fetch your product entities.然后,您可以像往常一样根据您的条件使用过滤器,添加限制、排序等并获取您的产品实体。

// <argument type="service" id="Shopware\Core\Content\ProductStream\Service\ProductStreamBuilder"/>
private ProductStreamBuilderInterface $productStreamBuilder;

// <argument type="service" id="sales_channel.product.repository"/>
private SalesChannelRepositoryInterface $productRepository;

// ..

$filters = $this->productStreamBuilder->buildFilters(
    $productStreamId,
    $salesChannelContext->getContext()
);

$criteria = new Criteria();
$criteria->addFilter(...$filters);

$products = $this->productRepository
    ->search($criteria, $salesChannelContext)
    ->getEntities();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM