# Verkaufskanal ID anhand Produkt Objekt per Code holen

**URL:** <https://forum.shopware.com/t/verkaufskanal-id-anhand-produkt-objekt-per-code-holen/103978>\
**Category:** Programmierung\
**Tags:** programming\
**Created:** [8. Mai 2024 um 15:56 UTC](https://forum.shopware.com/t/verkaufskanal-id-anhand-produkt-objekt-per-code-holen/103978 "2024-05-08T15:56:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas.kipp](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thomas.kipp](https://forum.shopware.com/u/thomas.kipp)\
**Post date:** [8. Mai 2024 um 15:56 UTC](https://forum.shopware.com/t/verkaufskanal-id-anhand-produkt-objekt-per-code-holen/103978/1 "2024-05-08T15:56:45Z")

</div>

Hallo,  
versuche durch folgenden Code die SalesChannel ID anhand eines product object zu holen, scheitere aber mit folgendem Code. Wie funktioniert das?

```auto
//this will fetch product object succesfully
$varientProduct = $this->productRepository->search($productCriteria, $context)->first();
//this will throw Exception
$varientProduct=$varientProduct->getSalesChannelId();

```

---

<div class="post-metadata">

**Author:** ![Anotherone](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Anotherone](https://forum.shopware.com/u/Anotherone)\
**Post date:** [8. Mai 2024 um 23:10 UTC](https://forum.shopware.com/t/verkaufskanal-id-anhand-produkt-objekt-per-code-holen/103978/2 "2024-05-08T23:10:49Z")

</div>

Die Exception wäre hilfreich, wenn ich mich nicht täusche hat das Product-Objekt aber keine Funktion getSalesChannelId, das geht über visibilities (ProductVisibilityCollection)

---

<div class="post-metadata">

**Author:** ![AlexGalax](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/alexgalax/32/9925_2.png) [@AlexGalax](https://forum.shopware.com/u/AlexGalax)\
**Post date:** [9. Mai 2024 um 05:42 UTC](https://forum.shopware.com/t/verkaufskanal-id-anhand-produkt-objekt-per-code-holen/103978/3 "2024-05-09T05:42:26Z")

</div>

Eine Produkt hat keine direkte SalesChannel-Association.  
In `$context` ist die Sales-Channel-Id aber drin. Einfach anschauen mit:

```php
dump($context);

```

---

<div class="post-metadata">

**Author:** ![thomas.kipp](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thomas.kipp](https://forum.shopware.com/u/thomas.kipp)\
**Post date:** [10. Mai 2024 um 08:54 UTC](https://forum.shopware.com/t/verkaufskanal-id-anhand-produkt-objekt-per-code-holen/103978/4 "2024-05-10T08:54:07Z")

</div>

Da es ein AJAX Call ist, konnte ich ich kein dd($context) setzen. Habe es jetzt probeweise so versucht, und die SalesChannelId bekommen:

```auto
            ob_start();        
            var_dump($productId);
            var_dump($varientProduct);
			var_dump($context->getSalesChannelId());
            //var_dump($context);
            if (file_put_contents( __DIR__. '/errorPlugin.log',PHP_EOL . ob_get_contents(), FILE_APPEND) === false){
                $temp_file = tempnam(sys_get_temp_dir(), 'Error');
                file_put_contents($temp_file, ob_get_contents(), FILE_APPEND);
            }
            ob_end_clean();

```

---

<div class="post-metadata">

**Author:** ![R4M](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/r4m/32/9983_2.png) [@R4M](https://forum.shopware.com/u/R4M)\
**Post date:** [10. Mai 2024 um 09:14 UTC](https://forum.shopware.com/t/verkaufskanal-id-anhand-produkt-objekt-per-code-holen/103978/5 "2024-05-10T09:14:37Z")

</div>

Hm, die SalesChannel ID über eine SQL-Abfrage ist keine Option? Und was passiert wenn das Produkt in mehreren Channels liegt?

---

<div class="post-metadata">

**Author:** ![thomas.kipp](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thomas.kipp](https://forum.shopware.com/u/thomas.kipp)\
**Post date:** [10. Mai 2024 um 11:39 UTC](https://forum.shopware.com/t/verkaufskanal-id-anhand-produkt-objekt-per-code-holen/103978/6 "2024-05-10T11:39:42Z")

</div>

Die SalesChannel ID habe ich wie im vorigen Post aufgezeigt bekommen. Habe vergessen, diesen Thread als gelöst zu markieren. Allerdings ergab sich daraus ein Folgethread: [Domains anhand eines Produktes auslesen](https://forum.shopware.com/t/domains-anhand-eines-produktes-auslesen/104000)

Zusatzfrage: Über welches Query würde ich denn die einem Produkt zugehörigen Domains bekommen? Das Ergebnis könnte ich dann in ein Array packen und entsprechend weiter verarbeiten. Kenne die involvierten Tabellen nicht, ansonsten könnte ich das Query selber formulieren.

---

<div class="post-metadata">

**Author:** ![AlexGalax](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/alexgalax/32/9925_2.png) [@AlexGalax](https://forum.shopware.com/u/AlexGalax)\
**Post date:** [12. Mai 2024 um 05:32 UTC](https://forum.shopware.com/t/verkaufskanal-id-anhand-produkt-objekt-per-code-holen/103978/7 "2024-05-12T05:32:35Z")

</div>

> [@thomas.kipp](#):
>
> Da es ein AJAX Call ist, konnte ich ich kein dd($context) setzen.

Wieso das denn? Der dump steht doch dann in der response.

> [@thomas.kipp](#):
>
> Zusatzfrage: Über welches Query würde ich denn die einem Produkt zugehörigen Domains bekommen? Das Ergebnis könnte ich dann in ein Array packen und entsprechend weiter verarbeiten. Kenne die involvierten Tabellen nicht, ansonsten könnte ich das Query selber formulieren.

ProductVisibilities → SalesChannels → Domains
