How to save and display custom field values from frontend in shopware6?

I have created a custom fieldset and a custom field from backend settings. 

I want to do the following

  1. Save the custom field value from the customer registration page.

  2. Display the custom field value in the customer profile page

  3. Update the custom field value on profile update

How to do these in shopware6?

1 „Gefällt mir“

My custom field name is name=“enable_abandoned_cart_notification_email”

I want to save the value during customer registration

Is it automatically save?

What will be the name(it is a checkbox field)?

name=“customer[customFields][enable_abandoned_cart_notification]”

or 

name=“customFields[enable_abandoned_cart_notification]” ? 

In shopware 5 it will be automatically saved(Free text field).

 

1 „Gefällt mir“

We are also dealing with that same problem. We want to extend the register form with a custom field, which was set up in backend. Have you already found a solution for that? We tried a lot of combinations in the register form, but none worked. From my point of view, the mapping function for custom_fields in the registration service isn´t implemented yet? 

Hi,

I am also struggling in this part. I am adding a text in the Custom field, and I want to do Data mapping with this field in the administration. Did anyone find a solution to that? 

1 „Gefällt mir“

@markenwirt, @mdoroci

My requirement was to save the custom field at the time of registration 

I did the following

In src/Resources/views/storefront/component/account/register.html.twig

{% sw_extends '@Storefront/storefront/component/account/register.html.twig' %}

{% block component_account_register_privacy %}
    {% block component_account_register_abandoned_cart_notification_control %}
    	{% if(shopware.config.AbandonedCartNotification.config.doNotAskEmailPermission==false) %}
		    
			    
			        {{ "abandonedCartNotification.register.abandonedCartNotificationCardTitle"|trans }}
			    
		        
		          {% block component_account_register_abandoned_cart_notification%}
		               
		          {% endblock %}

		          {% block component_account_register_enable_abandoned_cart_notification_label %}
		              
	        				{{ "abandonedCartNotification.register.enableAbandonedCartNotificationEmailText"|trans }}
		              
		          {% endblock %}
		        
		    
	    {% endif %}
	{% endblock %}

	{{ parent() }}
{% endblock %}

Created new file src/Subscriber/MappingRegisterCustomer.php

 */

namespace Hatslogic\Sw\AbandonedCartNotification\Subscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\Checkout\Customer\CustomerEvents;

class MappingRegisterCustomer implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'addCustomField'
        ];
    }

    public function addCustomField(DataMappingEvent $event)
    {

        $inputData = $event->getInput();

        $outputData = $event->getOutput();

        $enableAbandonedCartNotificationEmail = $inputData->get('enable_abandoned_cart_notification_email', false);

        $enableAbandonedCartNotificationEmail = ($enableAbandonedCartNotificationEmail)? true : false;
    
        $outputData['customFields'] = array('enable_abandoned_cart_notification_email' => $enableAbandonedCartNotificationEmail);

        $event->setOutput($outputData);

        return true;
    }
}

That’s it. it will automatically save the custom field.

Custom field values will be availabe in the variable “context.customer.customFields” (in view page)

context.customer.customFields.enable_abandoned_cart_notification_email

5 „Gefällt mir“

Thank you for sharing your code, what saved us a lot of time! For the sake of completeness, you need to mention, that the subscriber needs to be added in an extended services.xml ([yourPlugin]/src/Resources/config/services.xml) like: 

 

1 „Gefällt mir“

If you use CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => ‘addCustomField’ too in getSubscribedEvents() the thinggi works in customer account profile save, too! Thanks for your tipps.

1 „Gefällt mir“

Hello I have the same problem, but the custom fields ,i wanted to add at listing page, At backend its fine but not displaying at the storefront.

Thank you in advance

I know it’s late but for those coming along here, I have prepared an example plugin for this task: https://github.com/vanWittlaer/blog-example-two
Feedback welcome.

3 „Gefällt mir“

In Shopware 6.4.15 saving a custom field to a customer worked without the need for a subscriber:
In the registration form just add a field with follwing name:
name=“customFields[my_custom_field]”

I’ve tried this with the customer entity and having a checkbox on the profile page but it did not work. I’ve added the input to the form as per documentation (as well as my custom field modifieable by the store api) and the value gets transmitted with the post request but unfortunately, the value does not update.

Do you have an example for this?

I’ve tried this with the customer entity and having a checkbox on the profile page but it did not work. I’ve added the input to the form as per documentation (as well as my custom field modifieable by the store api) and the value gets transmitted with the post request but unfortunately, the value does not update.

This was because of a validation error the api did not communicate to me. My apologies

I have a similar problem: I created my own custom fields in the Admin backend and assigned them to orders. In the checkout, I integrated them via Twig (e.g., directly under the additional block), and the fields can be filled in there.

The problem: after completing the order, the fields are empty. When I open the order details in the Admin, I don’t see any values in the custom fields.

So far, I’ve managed to get the field values correctly attached to the order form, and they are also included in the POST /checkout/order. But Shopware does not automatically save them into order.customFields. That’s why I wrote a subscriber to catch the fields during checkout and write them into the order — but somehow it’s not working. :frowning:

You need to share your code otherwise, everyone can only guess what the problem might be. Shopware doesn’t automatically persist data from the objects customfields. You need to call the orderRepository and upsert the customfield data. Did you do that?

That’s my code:

<?php declare(strict_types=1);

namespace CryptoCustomField\Storefront\Controller;

use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class EndkundeController extends StorefrontController
{
    #[Route(
        path: '/checkout/endkunde/save',
        name: 'frontend.checkout.endkunde.save',
        methods: ['POST'],
        defaults: ['_routeScope' => ['storefront'], '_csrf_protected' => false]
    )]
    public function save(Request $request): JsonResponse
    {
        error_log('[EndkundeController] entry');

        try {
            $keys = [
                'custom_endkunde_name',
                'custom_endkunde_mail',
                'custom_endkunde_strasse',
                'custom_endkunde_plz',
                'custom_endkunde_stadt',
                'custom_endkunde_walletid',
                'custom_endkunde_miningpool',
            ];

            $payload = [];
            foreach ($keys as $k) {
                $payload[$k] = (string) $request->request->get($k, '');
            }

            $session = $request->hasSession() ? $request->getSession() : null;
            if ($session) {
                $session->set('custom_endkunde', $payload);
                // optional: $session->save();

                error_log('[EndkundeController] saved to session: ' . json_encode($payload));

                return new JsonResponse(['saved' => true, 'via' => 'session']);
            }

            error_log('[EndkundeController] no session available');
            return new JsonResponse(['saved' => false, 'error' => 'no-session'], 200);
        } catch (\Throwable $e) {
            error_log('[EndkundeController] ERROR: ' . $e->getMessage() . ' @ ' . $e->getFile() . ':' . $e->getLine());
            return new JsonResponse(['saved' => false, 'error' => 'internal'], 500);
        }
    }
}

And that’s the subscriber:

<?php

namespace CryptoCustomField\Subscriber;

use Shopware\Core\Checkout\Order\Event\OrderPlacedEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class EndkundeOrderPlacedSubscriber implements EventSubscriberInterface
{
    public function __construct(
        private readonly EntityRepository $orderRepository,
        private readonly RequestStack $requestStack
    ) {}

    public static function getSubscribedEvents(): array
    {
        return [
            OrderPlacedEvent::class => 'onOrderPlaced',
        ];
    }

    public function onOrderPlaced(OrderPlacedEvent $event): void
    {
        $request = $this->requestStack->getCurrentRequest();
        if (!$request) {
            return;
        }

        $values = $request->getSession()->get('custom_endkunde');
        if (!\is_array($values) || !$values) {
            return;
        }

        $orderId = $event->getOrderId();
        $context = $event->getContext();

        $update = [
            'id' => $orderId,
            'customFields' => [
                'custom_endkunde_name' => (string)($values['custom_endkunde_name'] ?? ''),
                'custom_endkunde_mail' => (string)($values['custom_endkunde_mail'] ?? ''),
                'custom_endkunde_strasse' => (string)($values['custom_endkunde_strasse'] ?? ''),
                'custom_endkunde_plz' => (string)($values['custom_endkunde_plz'] ?? ''),
                'custom_endkunde_stadt' => (string)($values['custom_endkunde_stadt'] ?? ''),
                'custom_endkunde_walletid' => (string)($values['custom_endkunde_walletid'] ?? ''),
                'custom_endkunde_miningpool' => (string)($values['custom_endkunde_miningpool'] ?? ''),
            ],
        ];

        $this->orderRepository->update([$update], $context);
        $request->getSession()->remove('custom_endkunde');
    }
}

The POST request is sent (visible in the network tab), but the server returns HTTP 500. My troubleshooting hasn’t produced any leads so far. I suspect the event subscriber is crashing. Any ideas what might be causing this?