Shopware 6中如何通过ProductPageLoadedEvent修改商品数据?配置后修改不生效的问题排查
ProductPageLoadedEvent in Shopware 6? Let me break down your issue and walk you through the fix step by step.
First: Clarifying ProductPageLoadedEvent Timing
To answer your core question: ProductPageLoadedEvent fires after the product page data is loaded, but before the template is rendered. That means it’s absolutely the right event to use for modifying product data before it’s displayed to the user—your initial approach is correct here.
Why Your Current Code Isn’t Working
Your code looks mostly right, but there are two likely reasons the changes aren’t showing up on the frontend:
1. You’re Not Handling Multilingual Fields Correctly
Shopware 6 stores product names (and other text fields) as multilingual translations. When you call $product->setName(), you might not be updating the translation for the current request’s language. The template typically renders the translated value, so your change isn’t being picked up.
2. Storefront Caching
Shopware 6’s Storefront heavily caches pages. Even if your code works, the old cached version of the product page might still be displayed.
Fixed Code Implementation
Here’s how to adjust your AddDataToPage class to properly modify multilingual fields and ensure changes are visible:
<?php declare(strict_types=1); namespace Swag\BasicExample\Service; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\Context; use Shopware\Storefront\Page\Product\ProductPageLoadedEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Psr\Log\LoggerInterface; class AddDataToPage implements EventSubscriberInterface { private $productRepository; private $logger; public function __construct( EntityRepositoryInterface $productRepository, LoggerInterface $logger ) { $this->productRepository = $productRepository; $this->logger = $logger; } public static function getSubscribedEvents(): array { return [ ProductPageLoadedEvent::class => 'onProductPageLoaded' ]; } public function onProductPageLoaded(ProductPageLoadedEvent $event): void { $product = $event->getPage()->getProduct(); $context = $event->getContext(); // Log original values for debugging $this->logger->info('Original product name: ' . $product->getName()); $this->logger->info('Original manufacturer number: ' . $product->getManufacturerNumber()); // 1. Modify multilingual product name $languageId = $context->getLanguageId(); $translations = $product->getTranslations(); if (isset($translations[$languageId])) { $translation = $translations[$languageId]; $translation->setName('Prefix Product Name - ' . $translation->getName()); $product->setTranslations($translations); } // 2. Modify non-multilingual field (manufacturer number) $product->setManufacturerNumber('Prefix Mfg# - ' . $product->getManufacturerNumber()); // Log updated values $this->logger->info('Updated product name: ' . $product->getName()); $this->logger->info('Updated manufacturer number: ' . $product->getManufacturerNumber()); // No need to call setProduct() again—objects are passed by reference // $event->getPage()->setProduct($product); // This line is redundant } }
Key Changes:
- Multilingual Field Handling: We target the translation for the current request’s language directly, ensuring the change is reflected in the frontend.
- Proper Logging: Replaced custom file logging with Shopware’s built-in
LoggerInterface(more reliable and integrated with Shopware’s logging system). - Removed Redundant Code: Since
$productis an object passed by reference, updating it automatically updates the product in the page object—no need to callsetProduct()again.
Additional Steps to Ensure Changes Show Up
Update Your Services Configuration:
Add theLoggerInterfaceargument to yourservices.xmlto inject the logger:<?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="Swag\BasicExample\Service\AddDataToPage" > <argument type="service" id="product.repository"/> <argument type="service" id="Psr\Log\LoggerInterface"/> <tag name="kernel.event_subscriber" /> </service> </services> </container>Clear Storefront Cache:
Run this command in your Shopware root directory to clear the Storefront cache:bin/console cache:clear bin/console storefront:cache:clearDisable Cache Temporarily (For Debugging):
If you’re still having issues, you can disable Storefront caching temporarily by adding this to your.envfile:APP_ENV=dev
Final Notes
- Always test changes in a development environment first.
- Make sure your plugin is activated in the Shopware Admin.
- If you’re modifying other product fields, check if they’re multilingual (like descriptions) and adjust the code accordingly.
内容的提问来源于stack exchange,提问作者sandip

