Does anyone knows to modify product data using ShopwareStorefrontPageProductProductPageLoadedEvent
?
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="SwagBasicExampleServiceAddDataToPage" >
<argument type="service" id="product.repository"/>
<tag name="kernel.event_subscriber" />
</service>
</services>
</container>
AddDataToPage.php
<?php declare(strict_types=1);
namespace SwagBasicExampleService;
use ShopwareCoreFrameworkDataAbstractionLayerEntityRepositoryInterface;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use ShopwareStorefrontPageProductProductPageLoadedEvent;
class AddDataToPage implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $productRepository;
/**
* @param EntityRepositoryInterface $productRepository
*/
public function __construct(
EntityRepositoryInterface $productRepository
)
{
$this->productRepository = $productRepository;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductsLoaded'
];
}
/**
* @param ProductPageLoadedEvent $event
* @return void
*/
public function onProductsLoaded(
ProductPageLoadedEvent $event
)
{
// the product is inside the page object
$productData = $event->getPage()->getProduct();
//modifying name
$this->log($productData->getName());
$productData->setName('Prefix Product Name' . $productData->getName());
$this->log($productData->getName());
//modifying ManufacturerNumber
$this->log($productData->getManufacturerNumber());
$productData->setManufacturerNumber('Prefix ManufacturerNumber' . $productData->getManufacturerNumber());
$this->log($productData->getManufacturerNumber());
$event->getPage()->setProduct($productData);
}
/**
* @param $message
* @return void
*/
private function log($message)
{
$logFileName = 'someFile.log';
file_put_contents(
$logFileName,
$message . PHP_EOL,
FILE_APPEND
);
}
}
After modifying the above mentioned changes it still shows the original data although
$event->getPage()->setProduct($productData);
I’m in doubt whether ProductPageLoadedEvent
is an after dispatching event or before dispatching the event.