abinjohnedamanaabinjohnedamana MemberComments: 36 Received thanks: 9 Member since: February 2019

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?

Answers

  • abinjohnedamanaabinjohnedamana MemberComments: 36 Received thanks: 9 edited January 9 Member since: February 2019

    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)?

    <input type="checkbox" class="custom-control-input" name="enable_abandoned_cart_notification_email" value="1"/>


    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).

        <input name="register[personal][attribute][abandonedCartNotification]"
                           type="checkbox"
                           id="abandoned_cart_notification"
                           value="1"
                           checked="checked"
                           />

     

    Thanked by 1markenwirt
  • markenwirtmarkenwirt MemberComments: 4 Received thanks: 1 Member since: January 21

    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? 

  • mdorocimdoroci MemberComments: 5 Received thanks: 2 Member since: November 2019

    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? 

    Thanked by 1markenwirt
  • abinjohnedamanaabinjohnedamana MemberComments: 36 Received thanks: 9 edited January 29 Member since: February 2019

    @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) %}
    		    <div style="margin-bottom:3rem;">
    			    <div class="card-title">
    			        {{ "abandonedCartNotification.register.abandonedCartNotificationCardTitle"|trans }}
    			    </div>
    		        <div class="custom-control custom-checkbox">
    		          {% block component_account_register_abandoned_cart_notification%}
    		               <input type="checkbox" class="custom-control-input" name="enable_abandoned_cart_notification_email" value="1" id="enableAbandonedCartNotificationEmail">
    		          {% endblock %}
    
    		          {% block component_account_register_enable_abandoned_cart_notification_label %}
    		              <label class="custom-control-label no-validation"
    		                     for="enableAbandonedCartNotificationEmail">
    	        				{{ "abandonedCartNotification.register.enableAbandonedCartNotificationEmailText"|trans }}
    		              </label>
    		          {% endblock %}
    		        </div>
    		    </div>
    	    {% endif %}
    	{% endblock %}
    
    	{{ parent() }}
    {% endblock %}

    Created new file src/Subscriber/MappingRegisterCustomer.php

    <?php declare(strict_types=1);
    /**
     * (c) 2Hats Logic Solutions <info@2hatslogic.com>
     */
    
    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

  • markenwirtmarkenwirt MemberComments: 4 Received thanks: 1 Member since: January 21

    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: 

    <container xmlns="http://symfony.com/schema/dic/services";
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
               xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">;
    
        <services>
            <service id="[yourPlugin]\Subscriber\MappingRegisterCustomer">
                <tag name="kernel.event_subscriber"/>
            </service>
        </services>
    </container>

     

    Thanked by 1digitalhoch3
  • digitalhoch3digitalhoch3 MemberComments: 22 Received thanks: 0 Member since: January 2015

    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.

Sign In or Register to comment.