Henning
Henning

Reputation: 23

Shopware 6.5 line-item with customfields

is there a way to get access to the custom fields in twig in the shopping cart in shopware 6.5? In Shopware 6.4 I could do this with lineItem.payload.customFields now in Shopware 6.5 this is empty. I can't find this under {{dump(lineItem)}}

Upvotes: 1

Views: 1580

Answers (2)

David
David

Reputation: 576

In case it is an app just add <allow-cart-expose> inside your field in manifest.xml file:

            <multi-entity-select name="example_field">
                <label>Example Field</label>
                <label lang="de-DE">Example Field</label>
                <placeholder>Choose an example product</placeholder>
                <entity>product</entity>
                <allow-cart-expose>true</allow-cart-expose>
            </multi-entity-select>

the only issue here is that it only shows the customFields in php, whereas the twig variables for lineItems are still missing the customFields

Upvotes: 0

dneustadt
dneustadt

Reputation: 13161

Update: Coming with 6.5.1.0 there will be the possibility to make custom fields exposed in carts again. This is a setting in the custom field configuration and it can be set when creating the custom field in an app/plugin or via the administration.


You now have to create a subscriber and subscribe to CartBeforeSerializationEvent to add allowed custom fields for exposition in the cart.

class CartBeforeSerializationSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            CartBeforeSerializationEvent::class => 'onCartBeforeSerialization',
        ];
    }

    public function onCartBeforeSerialization(CartBeforeSerializationEvent $event): void
    {
        $allowed = $event->getCustomFieldAllowList();
        $allowed[] = 'my_custom_field';
        
        $event->setCustomFieldAllowList($allowed);
    }
}

Upvotes: 3

Related Questions