How can we set custom fields per cart line item?

The CartLineItems have a property named payload where product custom fields and such things are stored within. In my case I used the LineItemAddedEvent to modify this payload.

e.g.

    /**
     * @param LineItemAddedEvent $event
     * @throws \Shopware\Core\Checkout\Cart\Exception\InvalidPayloadException
     */
    public function onLineItemAdded(LineItemAddedEvent $event): void
    {
        $lineItem = $event->getLineItem();
        foreach ($this->requestStack->getCurrentRequest()->get('lineItems') as $key => $item) {
            if ($lineItem->getId() == $key && isset($item['anotherCartFormValue'])) {
                $lineItem->setPayloadValue('anotherCartFormValue', $item['anotherCartFormValue']);
            }
        }

        // or do some other stuff with the payload
    }

This payload will be used in any step of the checkout without getting overwritten anymore, and is stored in the cart table.

4 „Gefällt mir“