# Preise fehlen beim Auslesen des Produkts

**URL:** <https://forum.shopware.com/t/preise-fehlen-beim-auslesen-des-produkts/105547>\
**Category:** Programmierung\
**Created:** [17. Oktober 2024 um 10:03 UTC](https://forum.shopware.com/t/preise-fehlen-beim-auslesen-des-produkts/105547 "2024-10-17T10:03:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![runi](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@runi](https://forum.shopware.com/u/runi)\
**Post date:** [17. Oktober 2024 um 10:03 UTC](https://forum.shopware.com/t/preise-fehlen-beim-auslesen-des-produkts/105547/1 "2024-10-17T10:03:44Z")

</div>

Hallo,

wir selektieren nach eigenen Kriterien Produkte und möchten diese dann darstellen.

Dazu haben wir nach dieser Anleitung

> **[Reading Data | Shopware Documentation](https://developer.shopware.com/docs/guides/plugins/plugins/framework/data-handling/reading-data.html)**
>
> Documentation for Shopware developers

die Produkte abgefragt. Das funktioniert soweit. Allerdings fehlen die Preis-Informationen.

Ich habe mal zum Vergleich auf der regulären Such-Seite gedumpt:  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/3/4/34a9e438923f699975268013699202987228aeb6.png)

Da sind Felder wie calculatedPrices, calculatedPrice, calculatedCheapestPrice, etc. enthalten.

Bei meinem Product fehlen diese Daten:

![image](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/d/1/d1243cfefc2b2c12dd126a3945675d964376a9b8.png)

Mein Code sieht so aus:

```auto
public function getProductById(SalesChannelContext $salesChannelContext, string $productId){
  $criteria = new Criteria();
  $criteria->addFilter(new EqualsFilter('id', $productId));
  $criteria->addAssociation('media');
  $criteria->addAssociation('cover');
  $criteria->addAssociation('prices');
  $criteria->addAssociation('tax');
  
  $product = $this->productRepository->search($criteria, $salesChannelContext->getContext())->first();
  
  return $product;
}

```

Was fehlt hier? Wie bekomme ich die calculated Preise in mein Produkt?

Für die Darstellung würde ich gerne Folgendes benutzen:

```auto
{% sw_include '@Storefront/storefront/component/product/card/box.html.twig' with {
                        layout: boxLayout,
                        displayMode: displayMode
                    } %}

```

Aber hier steht dann bei Preis 0 EUR. Hier wird auf calculatedPrice zugegriffen.

---

<div class="post-metadata">

**Author:** ![runi](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@runi](https://forum.shopware.com/u/runi)\
**Post date:** [17. Oktober 2024 um 13:13 UTC](https://forum.shopware.com/t/preise-fehlen-beim-auslesen-des-produkts/105547/2 "2024-10-17T13:13:07Z")

</div>

Hier die Lösung, falls es jemanden interessiert:

Der Hinweis von lukvai hier hat mich auf die richtige Fährte gebracht:

> <https://github.com/shopware/shopware/issues/3344>
>
> \### PHP Version
> 
> 8.1
> 
> \### Shopware Version
> 
> 6.5.5.2
> 
> \### Expected behaviour
> 
> Get… cheapest price of a product.
> 
> \### Actual behaviour
> 
> Property cheapestPrice do not exist in class Shopware\\Core\\Content\\Product\\ProductEntity
> 
> in \[vendor/shopware/core/Framework/DataAbstractionLayer/Entity.php \](file:///var/www/html/vendor/shopware/core/Framework/DataAbstractionLayer/Entity.php#L114)(line 114)
> 
> in \[vendor/shopware/core/Content/Product/SalesChannel/Price/ProductPriceCalculator.php \](file:///var/www/html/vendor/shopware/core/Content/Product/SalesChannel/Price/ProductPriceCalculator.php#L116)-\> get (line 116)
> 
> \### How to reproduce
> 
> \`\`\`
> use Shopware\\Core\\Content\\Product\\SalesChannel\\Price\\AbstractProductPriceCalculator;
> public function \_\_construct(
> private AbstractProductPriceCalculator $priceCalculator
> ) {}
> \`\`\`
> \`\`\`
> public function getProduct(String $id, SalesChannelContext $context)  
> {      
> $result = $this-\>productRepository-\>search(new Criteria(\[$id\]), $context-\>getContext());
> if (!$result-\>count()) {
> return false;
> }
> $product = $result-\>first();
> $this-\>priceCalculator-\>calculate(\[$product\], $context); // Errors happens here.
> return $product;
> }
> \`\`\`

In der service-Definition muss es

```auto
<argument type="service" id="sales_channel.product.repository"/>

```

statt

```auto
<argument type="service" id="product.repository"/>     

```

heißen.

In der Klasse dann auch entsprechend:

```auto
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
...
public function __construct(SalesChannelRepository $productRepository)
    {
...

```

Und in der search-function muss statt context salesChannelContext verwendet werden:

```auto
$product = $this->productRepository->search($criteria, $salesChannelContext)->first();

```

statt

```auto
$product = $this->productRepository->search($criteria, $salesChannelContext->getContext())->first();

```

Danke an lukvai 👍

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/2/9/29c7587ba660f00e4995d1743162782e5d6d8653.svg) [@system](https://forum.shopware.com/u/system)\
**Post date:** [16. November 2024 um 13:13 UTC](https://forum.shopware.com/t/preise-fehlen-beim-auslesen-des-produkts/105547/3 "2024-11-16T13:13:23Z")

</div>

Dieses Thema wurde automatisch 30 Tage nach der letzten Antwort geschlossen. Es sind keine neuen Antworten mehr erlaubt.
