Reading list Switch to dark mode

    How to get data according to sales channel using predefined event

    Updated 26 November 2021

    In this blog, you are going to learn “How to get data according to sales channel using predefined event.”
    I hope you know the directory structure of Shopware 6 plugin, if you don’t know, see here- https://docs.shopware.com/en/shopware-platform-dev-en/internals/directory-structure.

    The Purpose of using sales channel is some of the event does not have the sales channel context. So we need to add sales channel event to get data according to them. Sales Channel context is required for using core service like productListRoute service.

    Create an subscriber file for using the event, follow the directory structure : <plugin root>/src/Subscriber/TestSubscriber.php

    TestSubscriber.php
    <?php declare(strict_types=1);
    
    namespace Webkul\Test\Subscriber;
    
    
    use Shopware\Core\Content\Product\ProductEvents;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
    use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
    use Shopware\Core\Checkout\Customer\SalesChannel\LoginRoute;
    use Shopware\Core\Framework\Validation\DataBag\DataBag;
    
    class TestSubscriber implements EventSubscriberInterface
    {   
        /**
         * @var LoginRoute
         */
        protected $loginRoute;
    
        public function __construct(LoginRoute $loginRoute)
        {
            $this->loginRoute = $loginRoute;
        }
    
        public static function getSubscribedEvents()
        {
            return [
                ProductEvents::PRODUCT_LOADED_EVENT => 'updateLoadedProduct',
                'sales_channel.'. ProductEvents::PRODUCT_LOADED_EVENT => 'salesChannelLoadedProduct'
                
            ];
        }
    
        public function updateLoadedProduct(EntityLoadedEvent $event)
        {  
            //$event doesn't have sales channel
        }
    
        public function salesChannelLoadedProduct(SalesChannelEntityLoadedEvent $event)
        {   
           $saleChannelContext = $event->getSalesChannelContext();
           $dataBag = new DataBag();
           $dataBag->set('username', '[email protected]');
           $dataBag->set('password', 'test');
           $this->loginRoute->login($dataBag, $saleChannelContext);
        }
    }

    Add sales_channel in the event and use SalesChannelEntityLoadedEvent , this file load the data according to sales channel. In the above code, login route need data bag as first argument, salesChannelContext as second argument. If we use only EntityLoadedEvent we didn’t call login function because of this must use the sales channel.

    In Shopware, core service is now based on sales channel , there is two definition of product table. One of ProductDefiniton Webkul\MultiVendor\Core\Content\Bundle\ProductDefinition and second is SalesChannelProductDefinition Shopware\Core\Content\Product\SalesChannel\SalesChannelProductDefinition, this is reason we didn’t get sales channel like wise other table or service is done.

    Searching for an experienced
    Shopware Company ?
    Find out More

    To add the service follow the directory structure:- <plugin-root>/src/Resources/config/services.xml

    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="Webkul\Test\Subscriber\TestSubscriber" public="true">
                <tag name="kernel.event_subscriber" />
                <argument type="service" id="Shopware\Core\Checkout\Customer\SalesChannel\LoginRoute"></argument>
            </service>
        </services>
    </container>

    Add subscriber file in the service container so that Shopware knows this file and also add the login route service in the argument of that subscriber.

    Hope it will help you. Thanks for reading. Happy Coding 🙂
    Thank You.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home