'Shopware - how to save something to custom fields

I am creating an application (not plugin) to integrate with shipping companies. However, I can't deal with one problem - custom fields. I created one and linked it to an order. How can I save something to this field? It is called custom_parcel_locker_field. The template in which I need to display this field is: storefront/component/shipping/shipping-method.html.twig

Tried to insert something like this, but didn't work:

<input type="text" name="customFields['custom_parcel_locker_field']">
<input type="text" name="custom_parcel_locker_field">

I need the customer to be able to write something to this field.



Solution 1:[1]

I assume you want to save the custom field on the checkout page when a customer makes an order.

Extend the template shipping-method.html.twig by creating a new template file src/Resources/views/storefront/component/shipping/shipping-method.html.twig:

{% sw_extends '@Storefront/storefront/component/shipping/shipping-method.html.twig' %}

{% block extended_block_name %}
   {{ parent() }}

   <input type="text" name="customParcelLockerField" form="confirmOrderForm">
{% endblock %}

Shopware won't save this field automatically. You have to do this on your own using a subscriber. A good place to do this is for example the CartConvertedEvent. It is dispatched when customer's cart is converted to an order.

Create a new file src/Subscriber/CartConvertedSubscriber.php containing your subscriber class:

<?php declare(strict_types=1);

namespace YourNamespace\Subscriber;

use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class CartConvertedSubscriber implements EventSubscriberInterface
{
    private RequestStack $requestStack;

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

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

    public function addCustomFieldsToOrder(CartConvertedEvent $event): void
    {
        $orderData = $event->getConvertedCart();
        $orderCustomFields = $orderData['customFields'] ?? [];

        $customParcelLocker = $this->requestStack->getCurrentRequest()->request->get('customParcelLockerField');

        if ($customParcelLocker) {
            $orderCustomFields['custom_parcel_locker_field'] = $customParcelLocker;
        }

        $orderData['customFields'] = $orderCustomFields;

        $event->setConvertedCart($orderData);
    }
}

Then register your subscriber in src/Resources/config/services.xml:

<service id="YourNamespace\Subscriber\CartConvertedSubscriber">
   <argument type="service" id="request_stack"/>
   <tag name="kernel.event_subscriber"/>
</service>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1