Shopware 6:如何将具有变体的产品作为子产品添加到订单项中的父产品

Shopware6: How to add Products with variation as childrens to Parent Products in LineItems

我正在尝试在 shopware 6 中构建此页面:

但是由于在 shopware 中 6 种产品有差异(如下图)我不能那样做。

我需要将具有变体的产品分组到父产品下。 有人有想法吗?

我现在正在处理的订阅者:

use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class AddDataToPage implements EventSubscriberInterface

{   

public static function getSubscribedEvents()
{
    return [BeforeLineItemAddedEvent::class => 'onLineItemAdded'];
}

/**
 * @param onLineItemAdded $event
 * @throws \Shopware\Core\Checkout\Cart\Exception\InvalidPayloadException
 */
public function onLineItemAdded(BeforeLineItemAddedEvent $event)
{
   
    $lineitems = $event->getLineItem();
    

    // I need a [IF] here: if product has a variation and parent product id is the same add the code below
    $lineitems->setPayloadValue("myVar", "test2");
}

}

我建议采取以下步骤:

  1. 装饰AbstractCartItemAddRoute
  2. 获取将要添加的产品的父级产品,并为其添加广告
  3. 将实际变体添加为子项
  4. 添加 CartProcessor 或装饰 ProductProcessor 以计算第一级项目的价格,以及父产品。这应该是所有变体位置的总和

当然,您应该在第 2 步中检查父项或变体子项是否存在,然后更新它。

我已经实现了 BeforeLineItemAddedEvent 订阅者事件,但它没有被调用。 ProductPageLoadedEvent 被正确调用,但 BeforeLineItemAddedEvent 或 AfterLineItemAddedEvent 未被调用 谁能告诉我这有什么问题

这是我的订阅者 class

    <?php

namespace KiranPlugin\Subscriber;

use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class KiranSubscriber implements EventSubscriberInterface
{   

    public static function getSubscribedEvents(): array
    {
        return [
            BeforeLineItemAddedEvent::class  => 'beforeLineItemAdded',
            ProductPageLoadedEvent::class => 'onProductPageLoaded',
            AfterLineItemAddedEvent::class  => 'onLineItemAdded',
        ];
    }

    public function onLineItemAdded(AfterLineItemAddedEvent $event) 
    {
        echo 1;
        error_log('onLineItemAdded');
    }
    
    public function beforeLineItemAdded(BeforeLineItemAddedEvent $event) 
    {
        echo 'test';
            $mylineitems = $event->getCart()->getLineItems();
            
        error_log('beforeLineItemAdded');
    }
    
    /**
     * Handles the stuff, that happens on the product detail page
     *
     * @param ProductPageLoadedEvent $event
     */
    public function onProductPageLoaded (ProductPageLoadedEvent $event)
    {
        echo $sqlQuery = "SELECT * FROM `product`";
    
        //do something when the product detail page loads
    }
}