# How can we set custom fields per cart line item?

**URL:** <https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122>\
**Category:** Shopware 6 (English)\
**Created:** [3. Oktober 2019 um 07:58 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122 "2019-10-03T07:58:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![SorinSologics2019](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@SorinSologics2019](https://forum.shopware.com/u/SorinSologics2019)\
**Post date:** [3. Oktober 2019 um 07:58 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/1 "2019-10-03T07:58:08Z")

</div>

Hello,

Does anynone knows how can we set a custom field for a line item?

We notice that we can set per Payment, Shipping, ..but not for a cart line item.&nbsp;

We checked ‚Shopware\Core\Checkout\Cart\LineItem‘ but there are no setter/getter and no variable.

We need to save additional data to cart line item and we want this data to be persistent within order line items too.

Does anyone knows how to achieve that? Maybe by extending&nbsp;Shopware\Core\Checkout\Cart\LineItem and implement there setter/getter. We notice that ‚order\_line\_item‘ table does have a ‚custom\_fields‘ field.&nbsp;

Any help would be appreciated.

&nbsp;

Best regards,

Sorin

---

<div class="post-metadata">

**Author:** ![fulltimeSW](https://avatars.discourse-cdn.com/v4/letter/f/7cd45c/32.png) [@fulltimeSW](https://forum.shopware.com/u/fulltimeSW)\
**Post date:** [2. November 2019 um 15:36 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/2 "2019-11-02T15:36:39Z")

</div>

push

---

<div class="post-metadata">

**Author:** ![mii](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mii](https://forum.shopware.com/u/mii)\
**Post date:** [2. Dezember 2019 um 11:18 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/3 "2019-12-02T11:18:32Z")

</div>

Good question, any solution?&nbsp;

If i use Javascript fetch and the Shopware 6 - Sales-Channel API with&nbsp; **POST&nbsp; /sales-channel-api/v1/checkout/cart/product/{id}** from the Documentation\* to add products.&nbsp;But there is no example or explenation&nbsp;how to use the _payload_&nbsp;or how it sould look like to set/write the&nbsp;_customFields?_

To get/read the customFields this works fine:

```
fetch(serverUrl + '/sales-channel-api/v1/product/' + id + '?associations[properties][]', {
    'method': 'GET',
    'headers': getHeadersWithToken(false)
})...

```

regards, mii

\* Documentation:&nbsp;[https://docs.shopware.com/en/shopware-platform-dev-en/api/sales-channel-cart-api?category=shopware-platform-dev-en/api](https://docs.shopware.com/en/shopware-platform-dev-en/api/sales-channel-cart-api?category=shopware-platform-dev-en/api)

---

<div class="post-metadata">

**Author:** ![patchee500](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/patchee500/32/17600_2.png) [@patchee500](https://forum.shopware.com/u/patchee500)\
**Post date:** [9. Februar 2020 um 15:44 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/4 "2020-02-09T15:44:21Z")

</div>

Achieving this in the current state of shopware 6 seems to be quite a tedious task.

I was surprised this was rather difficult to achieve as it seems quite a common use case when working with eCommerce frameworks, especially within the enterprise&nbsp;sector.

To be able to store information on order item level I had to do the following (for sure this might not be&nbsp;best practice but it works, maybe it will help somebody):

1. Find a way to add custom data to your cart (line) items.

Line items are stored inside the cart table, to be specific in a serialized form inside the cart column.  
(This implementation for sure raises&nbsp;some questions but it is what it is, I guess)

You want to find a way to add custom data to the line items stored there. In my case I rewrote the CartLineItemController&nbsp;(\Shopware\Storefront\Controller\CartLineItemController::addLineItems) to store additional data on line item level.

1. Create a new Struct class (inheriting from&nbsp;\Shopware\Core\Framework\Struct\Struct)&nbsp;that will then be used to extend a line item&nbsp;(at the point where you add your custom data).

2. Add your custom data to the line item via your newly created custom struct. It works like this:

$lineItem-\>addExtension(‘my\_custom\_struct’, new MyCustomStruct($myCustomData));

1. Finally, as the custom data is now stored inside the cart item you want to make sure it gets copied to the order line items (table&nbsp;order\_line\_item) when the order gets placed.  
For that further rewriting is required.  
I rewrote&nbsp;\Shopware\Core\Checkout\Cart\Order\Transformer\LineItemTransformer::transform for this.  
You can utilize custom fields as per documentation ([https://docs.shopware.com/en/shopware-platform-dev-en/internals/core/data-abstraction-layer/custom-field](https://docs.shopware.com/en/shopware-platform-dev-en/internals/core/data-abstraction-layer/custom-field)), as they are supported for order line items.

if ($lineItem-\>getExtension(‘my\_custom\_struct’)) {  
&nbsp; &nbsp; $myCustomFieldData = [‘my\_custom\_field’ =\> $lineItem-\>getExtension(‘my\_custom\_struct’)-\>getMyCustomData()];  
}

$data = [  
&nbsp; &nbsp; ‘id’ =\> $id,  
&nbsp; &nbsp; ‘identifier’ =\> $lineItem-\>getId(),  
&nbsp; &nbsp; ‘productId’ =\> $productId,  
&nbsp; &nbsp; …  
&nbsp; &nbsp; ‘customFields’ =\> $myCustomFieldData  
];

That’s it. The custom data is now stored inside a custom field on the order line item and is accessible as per documentation.&nbsp;

---

<div class="post-metadata">

**Author:** ![mowlwurf](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mowlwurf](https://forum.shopware.com/u/mowlwurf)\
**Post date:** [10. Februar 2020 um 06:45 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/5 "2020-02-10T06:45:03Z")

</div>

The CartLineItems have a property named payload where product custom fields and such things are stored within. In my case I used the&nbsp;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.

---

<div class="post-metadata">

**Author:** ![michal\_bnet](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@michal\_bnet](https://forum.shopware.com/u/michal_bnet)\
**Post date:** [18. Februar 2020 um 07:30 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/6 "2020-02-18T07:30:48Z")

</div>

hi

is it possible to display this updated payload in order line item in admin ?

---

<div class="post-metadata">

**Author:** ![patchee500](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/patchee500/32/17600_2.png) [@patchee500](https://forum.shopware.com/u/patchee500)\
**Post date:** [25. Februar 2020 um 22:36 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/7 "2020-02-25T22:36:34Z")

</div>

> [@mowlwurf schrieb:](https://forum.shopware.com/profile/26263/mowlwurf "mowlwurf")
> 
> The CartLineItems have a property named payload where product custom fields and such things are stored within. In my case I used the&nbsp;LineItemAddedEvent to modify this payload.

Wow, how could I miss this. The payload approach is definitely less obtrusive and a lot easier to implement.

> is it possible to display this updated payload in order line item in admin ?

Yes. It comes with the order line item. You can display it in the backend by overwriting e.g. sw-order-detail-base.html.twig like this (might not look pretty, but you get the idea):

```
{% block sw_order_detail_base__user_card %}
    
        
            {{ lineItem.payload.myCustomPayload }}
        
    
    {% parent %}
{% endblock %}

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![claudio](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@claudio](https://forum.shopware.com/u/claudio)\
**Post date:** [1. März 2020 um 22:51 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/8 "2020-03-01T22:51:46Z")

</div>

Thanks for the pointers.

Another way to display it in the order-line-items-grid. Don’t know yet if there are any sideeffects though.

In e. g. ./MyPlugin/src/Resources/app/administration/src/main.js

```
const { Component } = Shopware;

Component.override('sw-order-line-items-grid', {
    computed: {

        getLineItemColumns() {
            const columns = this.$super('getLineItemColumns');

            columns.push({
                property: 'payload.myCustomPayload',
                dataIndex: 'payload.myCustomPayload',
                label: 'myCustomPayload',
                allowResize: false,
                primary: false,
                inlineEdit: false,
                width: '200px'
            });

            return columns;
        }
    }
});

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![afanelli](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@afanelli](https://forum.shopware.com/u/afanelli)\
**Post date:** [25. Mai 2020 um 09:06 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/9 "2020-05-25T09:06:20Z")

</div>

Hi!  
does anyone know if is it possibile to&nbsp;create&nbsp;a new rule for the builder based on custom&nbsp;values stored in this cart line item&nbsp;payload?

thanks

---

<div class="post-metadata">

**Author:** ![Homer0815](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@Homer0815](https://forum.shopware.com/u/Homer0815)\
**Post date:** [25. Oktober 2020 um 19:21 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/10 "2020-10-25T19:21:04Z")

</div>

@mowlwurf

Thank you so much!

---

<div class="post-metadata">

**Author:** ![fabiandingemans](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/fabiandingemans/32/26273_2.png) [@fabiandingemans](https://forum.shopware.com/u/fabiandingemans)\
**Post date:** [4. Juni 2025 um 20:30 UTC](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122/12 "2025-06-04T20:30:45Z")

</div>

Thank you so much.  
I was looking for so long for a solution.  
I ended up with this code. This gets the configurator\_data field from the request and saves it in the payload.

```auto
$lineItem = $event->getLineItem();
        foreach ($this->requestStack->getCurrentRequest()->get('lineItems') as $key => $item) {
            if ($lineItem->getId() == $key && isset($item['configurator_data'])) {
                $base64ConfiguratorData = $item['configurator_data'];
                $configuratorData = base64_decode($base64ConfiguratorData);
                $configuratorData = json_decode($configuratorData, true);

                $lineItem->setPayloadValue('configurator_data', $configuratorData);
            }
        }

```
