3

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

}

Arash Yazdani
  • 294
  • 3
  • 18
  • Are you talking about the shopping cart or the product detail page? What did you try so far? – Alex Jun 29 '21 at 18:21
  • I'm talking about shopping cart. when you add a product with variation to the cart it doesn't show it as a child of the parent product it shows it as a seperated product. and i need to group the product with variations so i can make the top image. so far i tried to make a Service and Subscriber to add custom field to products with variation but it is so complicated – Arash Yazdani Jun 30 '21 at 06:12
  • I think that's the right approach: In a subscribe colled the data and reoragnize it so you can easily print it in your template. Can you post the code you have so far? And do you get any error messages? – Alex Jun 30 '21 at 07:54
  • I'm trying to do this: 1. if products have a variation and first seven productNumber are the same add a customfield to payload 2. in twig file use that customfield using for loops and ... to make my page. i shared my code that i since now worked on. – Arash Yazdani Jun 30 '21 at 08:09
  • You are adding payload to the line item, it's unclear what you are exactly dumping in the twig? $lineItem->payload ? – Alex Jul 01 '21 at 12:44
  • 1
    Hi Alex, i've managed to solve that problem and i have shared my code on the top. what i need now and i don't know how to achive it is: i need a IF. if parentlineitem id is the same and products have variation add $lineitems->setPayloadValue("myVar", "test2"); . i have commented it on my code above. – Arash Yazdani Jul 01 '21 at 14:46

1 Answers1

1

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.

Skoenig
  • 437
  • 2
  • 9
  • Hi Skoenig, can you just give me an idea, how can i decorate AbstractCartItemAddRoute, I'm trying to do this: class AddDataToPage extends AbstractCartItemAddRoute and i get this: Class "Shopware\core\Checkout\Cart\SalesChannel\AbstractCartItemAddRoute" not found . can you please share some code. Thank You so much – Arash Yazdani Jul 05 '21 at 06:30
  • How does your service declaration looks like? – Skoenig Jul 05 '21 at 17:37
  • 1
    Well, this gist may will help you: https://gist.github.com/breaker92/f5ead2064d774c33dd15054be0b92e39 – Skoenig Jul 08 '21 at 19:54