Search code examples
shopwareshopware6

How to get sales channel ID from a product in Shopware 6?


Simple question, but I can't find a solution.

I'm trying to get the sales channel ID in Shopware 6 programmatically, but I'm not succeeding!

// This will fetch product object successfully
$product = $this->productRepository->search($productCriteria, $context)->first();

// This will throw Exception
$salesChannelId = $product->getSalesChannelId();

Any ideas on how to achieve this?


Solution

  • Add association to your criteria:

    $productCriteria->addAssociation('visibilities');
    

    The visibilities control in which sales channel the product should be visible.

    $product->getVisibilities();
    

    The result is displayed in the screenshot.

    Additional documentation that will help you: