如何在 Shopware 6 中使用 ProductPageLoadedEvent 修改产品数据?

发布于 2025-01-10 16:57:08 字数 3012 浏览 3 评论 0原文

有谁知道使用 Shopware\Storefront\Page\Product\ProductPageLoadedEvent 修改产品数据?

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="Swag\BasicExample\Service\AddDataToPage" >
                <argument type="service" id="product.repository"/>
                <tag name="kernel.event_subscriber" />
        </service>
    </services>
</container>

AddDataToPage.php

<?php declare(strict_types=1);

namespace Swag\BasicExample\Service;

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;

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
        );
    }
}


修改上述更改后,它仍然显示原始数据,尽管 $event->getPage()->setProduct($productData);

我怀疑 ProductPageLoadedEvent 是在分派事件之后还是在分派事件之前。

Does anyone knows to modify product data using Shopware\Storefront\Page\Product\ProductPageLoadedEvent ?

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="Swag\BasicExample\Service\AddDataToPage" >
                <argument type="service" id="product.repository"/>
                <tag name="kernel.event_subscriber" />
        </service>
    </services>
</container>

AddDataToPage.php

<?php declare(strict_types=1);

namespace Swag\BasicExample\Service;

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文