Search code examples
symfonysymfony5shopware

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


I'm trying to build this Page in shopware 6:

Product with Variation in Warencorb

but since in shopware 6 Products with Variation are sperated (picture below) i couldn't do that.

Symfony Shopware6 LineItems

I need to group Products with variation under Parent Product. Does anyone have an idea?

The Subscriber that I'm working on now:

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

}


Solution

  • I suggest the following steps:

    1. decorate AbstractCartItemAddRoute
    2. get parent product of the product which will be added, and ad it instead
    3. add the actually variant as a child
    4. add a CartProcessor or decorate the ProductProcessor to calculate the price of the first level item, with the parent product. Which should be the sum of all variant positions

    Of course you should check in step 2. if the parent or variant child line item exists and update it instead.