How to get the sales channel url in Shopware 6?

Viewed 1407

How to get the sales channel URL in Shopware 6 php, e.g. in a scheduled task. Which object does keep such information?

2 Answers

The URL of a sales channel is stored in the associated SalesChannelDomainEntity object. It is a one to many association, so a sales channel can have multiple domains.

For example to get the URL of the first domain use:

$url = $salesChannel->getDomains()->first()->getUrl();

I actually found a way to get all of the urls:

// retrieve all urls
        $urls = [];
        $salesChannelRepository = $this->container->get('sales_channel.repository');
        $criteria = new Criteria();
        $criteria->addAssociation('domains');
        $salesChannelIds = $salesChannelRepository->search($criteria, Context::createDefaultContext());
        foreach($salesChannelIds->getEntities()->getElements() as $key => $salesChannel){
            foreach($salesChannel->getDomains()->getElements() as $element){
                array_push($urls, $element->getUrl());
            }
        }
Related