How to Update Order custom field in Shopware 6 during order process?

Viewed 659

I want to add a selector for date during order process in checkout. In which step should the custom field of an order be updated? And how is the custom field update done for order or other entity? I want to add the fields as it is shown in official docs.

$this->customFieldSetRepository->create([
    [
        'name' => 'swag_example',
        'customFields' => [
            ['name' => 'swag_example_size', 'type' => CustomFieldTypes::INT],
            ['name' => 'swag_example_color', 'type' => CustomFieldTypes::TEXT]
        ]
    ]
], $context);
1 Answers

Where exactly do you want to add that data during the order process?

One example would be from confirm to finish -> the cart is converted to an order. You just need to create a Subscriber for that:

<?php

class OrderProcessSubscriber implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [
            CartConvertedEvent::class => 'addCustomFieldsToConvertedCart',
        ];
    }

    public function addCustomFieldsToConvertedCart(CartConvertedEvent $event)
    {
        $convertedCart['customFields']['my_custom_field'] =  'test';
        $event->setConvertedCart($convertedCart);
    }
}

?>
Related