# 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:** 1\
**Showing post:** 4

<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;

---

_[View the full topic](https://forum.shopware.com/t/how-can-we-set-custom-fields-per-cart-line-item/62122)._
