You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6中如何通过ProductPageLoadedEvent修改商品数据?配置后修改不生效的问题排查

How to Modify Product Data with 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 $product is an object passed by reference, updating it automatically updates the product in the page object—no need to call setProduct() again.

Additional Steps to Ensure Changes Show Up

  1. Update Your Services Configuration:
    Add the LoggerInterface argument to your services.xml to 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>
    
  2. Clear Storefront Cache:
    Run this command in your Shopware root directory to clear the Storefront cache:

    bin/console cache:clear
    bin/console storefront:cache:clear
    
  3. Disable Cache Temporarily (For Debugging):
    If you’re still having issues, you can disable Storefront caching temporarily by adding this to your .env file:

    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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 14:32:48