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