1

I created a bare minimum Shopware 6 plugin to display product ID when the product is loaded. It worked fine. Below is my code.

   PATH: src/Resources/config/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="TestPlugin\Listener\ProductLoadedListener" >
            <tag  name="kernel.event_listener" event="product.loaded" />
        </service>

    </services>
</container>

Below is the ProductLoadedListener.php codes

PATH: src/Listener/ProductLoadedListener.php

<?php declare(strict_types=1);

namespace TestPlugin\Listener;

use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;

class ProductLoadedListener{
    


    public function onProductLoaded(EntityLoadedEvent $entityLoadedEvent){
        
        print_r($entityLoadedEvent->getIds());
    

    }

}

The above codes did the job it was created to do. So I updated the ProductLoadedListener.php codes

<?php declare(strict_types=1);

namespace TestPlugin\Listener;


use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;

class ProductLoadedListener{
    


    public function onProductLoaded(Price $price){
        
        print_r($price->getNet());
    

    }

}

I go an error

Argument 1 passed to TestPlugin\Listener\ProductLoadedListener::onProductLoaded() must be an instance of Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price, instance of Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent given, called in /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php on line 270

So I am asking why I got the above error, I was expecting it to echo the net price?

DevOPlug
  • 59
  • 8

1 Answers1

3

Shopware will inject in the onProductLoaded function an EntityLoadedEvent object, not a Price object. That's why PHP throws this error.

If you want the get the price of the loaded product, then you should get the product from the $entityLoadedEvent and then get the price:

class ProductLoadedListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded'
        ];
    }

    public function onProductLoaded(EntityLoadedEvent $entityLoadedEvent)
    { 
        /** @var ProductCollection $loadedProducts */
        $loadedProducts = $event->getEntities();
        $firstProduct = $loadedProducts->first();
        $productNetPrice = $firstProduct->getPrice()->first()->getNet();
        dd($productNetPrice);
    }
}
Paweł Napierała
  • 1,631
  • 10
  • 15
  • @Pawal Napierala Thanks for the answer. I have questions about your code. What does the `getSubscribedEvents()` method do and can you show me any resource I can learn more about related concepts. Next, where did the `$event` and `$loadedProducts` object come from? – DevOPlug Feb 12 '22 at 18:22
  • @Destini you can find more information about the event subscribers and the `getSubscribedEvents()` function in [the Shopware documentation](https://developer.shopware.com/docs/guides/plugins/plugins/plugin-fundamentals/listening-to-events) and in [the Symfony documentation](https://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber), which is a framework used in Shopware. – Paweł Napierała Feb 13 '22 at 17:12
  • I have implemented your codes. I had to do an in-depth reading on "Symfony Framework". – DevOPlug Feb 25 '22 at 06:32