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?

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 Like

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 Like

@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

4 Likes

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 Like

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 Like

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