0
votes

how can I get the names of the dynamic product groups of a product? Especially the names of that groups. The names are in the table 'product_stream_translation'. I have a subscriber and have added this Criteria. When I am doing it like this:

$criteria->addFilter(new EqualsFilter('id', $id));
$criteria->addAssociation('streams');
$dynamicProductGroups = $this->productRepository->search($criteria, $context)->getEntities();

I just got back an empty streamEntity.

#streams: Shopware\Core\Content\ProductStream\ProductStreamCollection {#11805 ▼
    #elements: []
    #extensions: []
  }

When I am doing it like this:

$criteria->addFilter(new EqualsFilter('productId', $id));
$dynamicProductGroups = $this->productStreamMappingRepository->searchIds($criteria, $context);

I just got back the Id I put in: product_stream_mapping

I wonder how I will get the name of the dynamic product group. With a query I can get all the assign 'product_stream_id's from the table 'product_stream_mapping' like this:

SELECT * FROM product_stream_mapping WHERE product_id =0x000000000000000000123b313030524b

And then get the associated name of the dynamic product group. With is like this:

SELECT psm.product_id, psm.product_stream_id, p.product_number, pst.name 
FROM product_stream_mapping psm 
JOIN product_stream_translation pst ON pst.product_stream_id = psm.product_stream_id
JOIN product p ON p.id = psm.product_id
WHERE psm.product_id = 0x000000000000000000123b313030524b

How can I get it in the Subscriber? Do I have to use criteria or do I have to user a repository?

1

1 Answers

0
votes

This line is wrong:

$dynamicProductGroups = $this->productRepository->search($criteria, $context)->getEntities();

It won't return dynamic product groups but a product collection. To get a product stream collection (dynamic product groups) replace it with:

/** @var ProductEntity $product */
$product = $this->productRepository->search($criteria, $context)->first();
$dynamicProductGroups = $product->getStreams();

Then you can read the names of the streams with:

$names = array_values(
    $dynamicProductGroups->map(fn (ProductStreamEntity $productStream) => $productStream->getName())
);