Read More
Read More
Menu Close

    How to Subscribed Event in Shopware 6

    Introduction

    In this blog, we learn about the subscribed events in Shopware 6 at the storefront and pass custom data on this page. So let’s begin.

    Overview

    While creating a module, we need to subscribe event for Shopware 6 and how to pass data on this page.

    Page Overriding

    We take an example for the subscribed event, so we start to override the order page of the storefront. Suppose we want to override the order item list for adding custom order history for ordered items.

    First of all, we inspect the override area of HTML and copied any HTML content for searching in the core file. We reached the main content file and find the block of content which you want to override.

    You need to make some directories of the core files in your module under Resources/views. Like order item list exist in the storefront/page/account/order-history/order-detail-list-item.html.twig.

    Start your headless eCommerce
    now.
    Read More

    Then the same directory we should create in our module. After creating order-detail-list-item.html.twig file in the same directory as the main storefront file we need to extend the core storefront file.

    For overriding the core block, we need to copy and paste of needed block under the block. Call the parent method for existing content or core content as below.

    {% sw_extends '@Storefront/storefront/page/account/order-history/order-detail-list-item.html.twig' %}
    
    {% block page_account_order_item_detail_variants %}
        {{parent()}}
        Here you can write your custom content
    {% endblock %}

    Passing data

    If you want to pass your own data to order page then you need to subscribed orderPageLoadedEvent. Create a subscriber file in subscribers folder name of folder and file choose anything. But you need to register this in service.xml file under Resources/config folder.

    <service id="Webkul\MultiVendor\EventSubscriber\ShopwareSubscriber">
       <tag name="kernel.event_subscriber" />
       <argument type="service" id="service_container" />
    </service>

    You can pass arguments in service file from between service tag. Once you register subscriber in service xml file now subscriber working in module. Let’s subscribed AccountOrderPageLoadedEvent::class within getSubscribedEvents() function.

    After subscribed event we create a new function on event orderPageLoadedEvent() function like the below.

    public static function getSubscribedEvents()
        {
            return [
                AccountOrderPageLoadedEvent::class => 'orderPageLoadedEvent'
            ];
        }

    After creating a function we perform action for storing data into variable and pass through event object with addExtension method.

     public function orderPageLoadedEvent(AccountOrderPageLoadedEvent $event)
        {
            $marketplaceOrderRepository = $this->container->get('marketplace_order.repository');
           $marketplaceOrderLineItem = $marketplaceOrderRepository->search((new Criteria()), Context::createDefaultContext())->getElements();
            
           $shopwareOrder = $event->getPage()->getOrders()->getElements();
            foreach($shopwareOrder as $order) {
                foreach($order->getLineItems() as $lineItem) {
                    foreach($marketplaceOrderLineItem as $mpOrder) {
                        
                        if ($lineItem->getId() === $mpOrder->get('orderLineItemId')) {
                            $lineItem->addExtension('itemStatus', new IdStruct($mpOrder->get('state_machine_state')->getName()) );
                        }
                    }
                   
                }
            }
            
        }

    I hope it will help you. Thanks for reading. Happy Coding 🙂

    Thank You.

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    Be the first to comment.

    Back to Top