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

@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“