Shopware 6 Plugin Api get Product data with selected field to reduce response payload

Viewed 40

We created a plugin and now we want to get selected fields only on the response, to minimize the response payload.

We are aware that criteria "includes" will help based on the URL https://developer.shopware.com/docs/guides/integrations-api/general-concepts/search-criteria

In the Array of $criteria we can see what "includes" is added like filter but does not give the selected fields

Please help what I am doing wrong?

EXample

use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
$Criteria = new Criteria();
$Criteria->addFilter(new EqualsFilter('id', $productId));
$Criteria->setIncludes(array('product-alias' => array('name')));
$productRepo = $this->productRepository->search($Criteria, $context);

I have tried

$Criteria->setIncludes(array('product' => array('name')));
$Criteria->setIncludes(array('name'));
1 Answers

The includes collection is only used when serializing the objects for the response json, as you can find in the JsonApiEncoder. It's not in effect when fetching mapped objects internally, as it could lead to conflicts with only partially mapped objects. You could of course just use the JsonApiEncoder internally to encode a collection of entities to receive a response object including a subset of the data.

Related