Solved: ChildLineItems are set to quantity 0

There is a bug in Shopware 6, where the quantity of ChildLineItems is set to 0 if any discounts are present.

The reason is that for the discount calculation, the items are „attempted“ to be cloned in LineItem::createFromLineItem(). As ChildLineItems are objects, there are not copied but referenced.

The solution is a decorator:

class LineItemQuantitySplitterDecorator extends LineItemQuantitySplitter
{
public function getDecorated()
{
    return LineItemQuantitySplitter::class;
}

public function split(LineItem $item, int $quantity, SalesChannelContext $context): LineItem
{
    $item = clone($item);
    return parent::split($item, $quantity, $context);
}
}

This post is made in the hope that it pops up in the search of another dev - I just spent 12 hours hunting down that bug.