# Ausgabe eines Produktes in einem Template

**URL:** <https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041>\
**Category:** Programmierung\
**Created:** [26. Februar 2021 um 15:47 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041 "2021-02-26T15:47:08Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [26. Februar 2021 um 15:47 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/1 "2021-02-26T15:47:08Z")

</div>

Hallo,

ich würde gerne an einer bestimmten Stelle in meinem Twig Template ein Produkt (product card?)&nbsp;ausgeben. Die ID habe ich in einer Variable. Kurzer Denkanstoß oder wo ich mir das mal anschauen kann würde schon reichen 🙂

Lg Alex

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [2. März 2021 um 21:55 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/2 "2021-03-02T21:55:00Z")

</div>

Keiner eine Ahnung? ☹

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [19. März 2021 um 07:44 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/3 "2021-03-19T07:44:59Z")

</div>

Ich pushe den Beitrag nochmal, in der Hoffnung auf Hilfe ☹

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [22. März 2021 um 16:24 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/4 "2021-03-22T16:24:17Z")

</div>

Ich weiß mittlerweile, dass ich über den Code-Schnipsel  
`{% sw_include '@Storefront/storefront/component/product/card/box.html.twig' with {'layout': boxLayout, 'displayMode': displayMode } %}`

eine Product Card einfügen kann. Aber benötigt wird das Object „product“, das irgendwo befüllt wird. Gibt es so eine Funktion, die im Twig implementiert ist?

LG

---

<div class="post-metadata">

**Author:** ![Moorleiche](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Moorleiche](https://forum.shopware.com/u/Moorleiche)\
**Post date:** [23. März 2021 um 01:08 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/5 "2021-03-23T01:08:54Z")

</div>

Du brauchst einen Subscriber, dieser Subscriber muss die ID des Produktes kennen (z.B. via SystemConfigService). Und braucht den Service `product.repository`.

[https://docs.shopware.com/en/shopware-platform-dev-en/how-to/register-subscriber](https://docs.shopware.com/en/shopware-platform-dev-en/how-to/register-subscriber)

Je nachdem wo die Box hin soll musst du das Event kennen. HeaderPageletLoadedEvent wird z.B. auf jeder Storefront Seite geladen. CMS Elemente sind da etwas komplexer, da brauchst du einen CmsElementResolver.

Mit diesem Subscriber musst du eine ProductEntity erstellen und als Extension einem beliebigen Struct des Events hinzufügen… z.B. `$event->getSalesChannelContext()->addExtension('meinProdukt', $productEntity)` dann kann man das Produkt im Twig-Template vor dem include laden `{% set product = context.extensions.meinProdukt %}`…

Viel Spaß 😉

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [23. März 2021 um 08:37 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/6 "2021-03-23T08:37:40Z")

</div>

Auweija 😃 Danke für die ausführliche Erklärung, ich schaue mir das mal an.

Ich habe im Katalog bei den übergeordneten Menüpunkten ein Custom Field. Wenn dort eine Produkt-ID eingetragen ist, wird das Flyout Menü für diesen Menüpunkt nur 3-spaltig, statt 4-spaltig. Dafür soll in der 4. Spalte dann das jeweilige Produkt mit der angegeben ID ausgeben werden.

Das bedeutet, ich brauche das Event HeaderPageletLoadedEvent richtig?

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [20. April 2021 um 22:42 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/8 "2021-04-20T22:42:09Z")

</div>

Hallo Moorleiche,

ich bin diesbezüglich etwas weiter gekommen und habe mein erstes Plugin geschrieben.  
Ich hole mir ein Produkt über das product.repostory via  
`$product = $this->productRepository->search(new Criteria([$productID]), $context)->first();`

Anfangs hatte ich das Ganze zu Testzwecken an den HeaderPageletLoadedEvent subscribed. Das habe ich wieder entfernt, weil ich das ganze ja dann in meiner Klasse aufgerufen werden soll, die die Twig Extension erstellt.

Dazu habe ich eine Klasse mit AbstractExtension etweitert und meine Twig Extension registriert, die als Parameter sowohl eine Produkt-ID als auch context.context bekommt. Dort Instanziere ich dann meine Produktsuch-Klasse und gebe im Construktur sowohl $productRepository als auch meinen $context mit. Ich rufe die Methode auf, die mir das Produkt holt und gebe die Produkt ID mit.

Meine services.xml:

```
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="Bs\TwigGetProduct\Storefront\Pagelet\Header\Subscriber\AddTwigFunction">
            <tag name="twig.extension"/>
            <argument type="service" id="product.repository"/>
        </service>
    </services>
</container>

```

Das ganze funktioniert super. Er findet das Produkt und gibt es mir auch korrekt zurück.

```
{% set product1 %}
{{ getProductByID('2a88d9b59d474c7e869d8071649be43c', context.context) }}
{% endset %}

{{ dump(product1) }}

```

Das Ergebnis meines dumps, zeigt mir, was falsch läuft. Klassisch ist ja ein Produkt ein Objekt vom Typ SalesChannelProductEntity. Mein Objekt ist aber vom Typ ProductEntity. Wie kann ich statt einem ProductEntity ein SalesChannelProductEntity bekommen?

Danke für den Ansatz, hat mir weitergeholfen 🙂

Lg Alex

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [24. April 2021 um 16:00 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/9 "2021-04-24T16:00:36Z")

</div>

Habs mittlerweile gelöst.

---

<div class="post-metadata">

**Author:** ![Rednaxela](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Rednaxela](https://forum.shopware.com/u/Rednaxela)\
**Post date:** [7. Juli 2021 um 12:44 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/10 "2021-07-07T12:44:17Z")

</div>

@AlexBS Ich stehe gerade vor dem selben Problem, habe ein ProductEntity, brauche aber ein SalesChannelProductEntity. Du schreibst du hast es gelöst, kannst du mir da vielleicht auch weiterhelfen? Danke schon mal

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [11. Juli 2021 um 21:20 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/11 "2021-07-11T21:20:55Z")

</div>

Ich habe in der services.xml einfach  
`<argument type="service" id="sales_channel.product.repository"/>`  
statt id=„product.repository“ übergeben.

Und dann in der Exension Klasse

```auto
/**
* @var SalesChannelRepositoryInterface
*/
 private $salesChannelProductRepository;  

/*
 * Construct
*/
public function __construct(SalesChannelRepositoryInterface $salesChannelProductRepository)
{
    $this->salesChannelProductRepository = $salesChannelProductRepository
}

```

Hoffe ich kann dir damit helfen.

Lg Alex

---

<div class="post-metadata">

**Author:** ![Rednaxela](https://avatars.discourse-cdn.com/v4/letter/r/ad7895/32.png) [@Rednaxela](https://forum.shopware.com/u/Rednaxela)\
**Post date:** [12. Juli 2021 um 05:58 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/12 "2021-07-12T05:58:29Z")

</div>

Super, danke dir, ich habe vorher nur im Subscriber mir den SalesChannelContext geholt, leider ohne es auch schon in der services.xml zu ändern. Jetzt klappt es auch.  
Ich wünsche noch einen sonnigen Tag 🙂  
Alex

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [6. August 2021 um 09:08 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/13 "2021-08-06T09:08:56Z")

</div>

Gerne. Wir Alexe müssen uns ja gegenseitig helfen! 🙂  
Lg Alex

---

<div class="post-metadata">

**Author:** ![halabalusa](https://avatars.discourse-cdn.com/v4/letter/h/d07c76/32.png) [@halabalusa](https://forum.shopware.com/u/halabalusa)\
**Post date:** [30. August 2021 um 16:29 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/14 "2021-08-30T16:29:22Z")

</div>

Moin @AlexBS,

ich hoffe du hilfst mir, auch wenn ich kein Namensvetter von euch bin 😉

Ich versuche gerade die selbe Twig Extension zu bauen. Das zweite Argument (context.context), dass ich neben der ID an „search“ übergebe, macht allerdings Probleme:

```auto
Argument 2 passed to Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository::search() must be an instance of Shopware\Core\System\SalesChannel\SalesChannelContext, instance of Shopware\Core\Framework\Context given

```

Hier stehe ich auf dem Schlauch ☹ Aktuell sieht meine Twig-Extension so aus:

```auto

declare(strict_types=1);

namespace Halabalusa\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

class Productinfo extends AbstractExtension
{

  /**
   * @var SalesChannelRepositoryInterface
   */
  private $salesChannelProductRepository;

  /*
  * Construct
  */
  public function __construct(SalesChannelRepositoryInterface $salesChannelProductRepository)
  {
    $this->salesChannelProductRepository = $salesChannelProductRepository;
  }

  public function getFunctions()
  {
    return [
      new TwigFunction('getProductByID', [$this, 'searchProductByID']),
    ];
  }

  public function searchProductByID($productID, $context)
  {
    return
      $this->salesChannelProductRepository->search(new Criteria([$productID]), $context)->first();;
  }
}

```

Muss ich noch etwas mit _SalesChannelContext_ machen? Vielleicht hast du ja einen Hinweis für mich, das würde mir super helfen!

Danke und viele Grüße!

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [30. August 2021 um 19:19 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/15 "2021-08-30T19:19:00Z")

</div>

Hey,

versuchs mal mit „context“ statt „context.context“.

Lg  
Alex

---

<div class="post-metadata">

**Author:** ![halabalusa](https://avatars.discourse-cdn.com/v4/letter/h/d07c76/32.png) [@halabalusa](https://forum.shopware.com/u/halabalusa)\
**Post date:** [30. August 2021 um 19:22 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/16 "2021-08-30T19:22:04Z")

</div>

Hey @AlexBS , vielen Dank! Das hatte ich gerade schon getestet. Mit

```auto
{% set product1 %}
	{{ getProductByID('1403141fa6bc43428d5941a6caf50da6', context) }}

	{% endset %}

	{{ dump(product1) }}

```

gibt mir der dump aber

```auto
Twig\Markup {#10459 ▼
  -content: """
    \t
    
    \t
    """
  -charset: "UTF-8"
}

```

aus 🤔 Ich hab es doch richtig verstanden, dass kein Subscriber nötig ist, oder?

Viele Grüße!

---

<div class="post-metadata">

**Author:** ![halabalusa](https://avatars.discourse-cdn.com/v4/letter/h/d07c76/32.png) [@halabalusa](https://forum.shopware.com/u/halabalusa)\
**Post date:** [30. August 2021 um 19:32 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/17 "2021-08-30T19:32:17Z")

</div>

Ok, hat sich erledigt. Mit

```auto
{{ dump(getProductByID('1403141fa6bc43428d5941a6caf50da6', context)) }}

```

bekomme ich das Produkt. Verstehe nicht, wieso das mit dem _set_ nicht klappt, aber im Grunde egal, so komme ich weiter. Vielen Dank!

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [30. August 2021 um 19:38 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/18 "2021-08-30T19:38:18Z")

</div>

Hey,

das Problem hatte ich auch schon. Mit set kann man wohl nur Strings setzen.  
Funktioniert es jetzt?

Lg

---

<div class="post-metadata">

**Author:** ![halabalusa](https://avatars.discourse-cdn.com/v4/letter/h/d07c76/32.png) [@halabalusa](https://forum.shopware.com/u/halabalusa)\
**Post date:** [30. August 2021 um 19:50 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/19 "2021-08-30T19:50:15Z")

</div>

Ja, jetzt klappt alles. Ich bekomme meine Product Box angezeigt. Perfekt! Danke dir, ohne deine Posts hätte ich das nicht so schnell hinbekommen!

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [30. August 2021 um 21:13 UTC](https://forum.shopware.com/t/ausgabe-eines-produktes-in-einem-template/73041/20 "2021-08-30T21:13:49Z")

</div>

Freut mich dir geholfen zu haben.
