Page History

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. In Magento, navigate to Stores > Configuration > Gigya Identity Management > Field Mapping
  2. Specify the full path and file name of the field mapping JSON file.
  3. Click Save Config


Use Hook to Transform Data

In some cases, you may want to map fields between Gigya and Magento that have different types, or that have different logic. For example, gender in Magento is an integer, with 3 possible values: "1" for male, "2" for female, "3" for undefined. The corresponding Gigya gender field is a string, that accepts "m", "f" and "u". An example of a field having the same type but different logic is date of birth: at Gigya, this is comprised of 3 separate fields, one each for day, month and year. At Magento, this is one field. 

The GConnector includes two hooks that can be used to transform the data in these cases. 

Hook: gigya_pre_field_mapping

Fired upon synchronization from Gigya to Magento. 

Parameters:

  • gigya_user - the Gigya user object.
  • customer - an empty Magento user object.

 

Hook: pre_sync_to_gigya

Fired upon synchronization from Magento to Gigya. 

Parameters:

  • customer the Magento user object.
  • gigya_user an empty Gigya user object.

 

The following is an example of how to add custom data transformation hooks.

Default CMS Sync Field Mapping

Code Block
languagephp
themeMidnight
<?php
/**
 * Copyright
© 2016 X2i.
 */

namespace Gigya\GigyaIM\Observer;

use Gigya\CmsStarterKit\user\GigyaUser;
use Gigya\CmsStarterKit\user\GigyaProfile;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Data\Customer;
use Magento\Framework\Event\Observer;

/**
 * DefaultCMSSyncFieldMapping
 *
 * Default g2cms field mapping implementation. For now only attributes gender and date of birth (dob)
 *
 * To be effective one have to declare this observer on event 'gigya_pre_field_mapping'.
 */
class DefaultCMSSyncFieldMapping implements ObserverInterface
{
    /**
     * Method execute
     *
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        /** @var GigyaUser $gigyaUser */
        $gigyaUser = $observer->getData('gigya_user');

        /** @var GigyaProfile $gigyaProfile */
        $gigyaProfile = $gigyaUser->getProfile();
        /** @var Customer $magentoCustomer */
        $magentoCustomer = $observer->getData('customer');

        // 'Translate' the gender code from Gigya to Magento value
        switch ($gigyaProfile->getGender()) {
            case 'm':
                $magentoCustomer->setGender('1');
                break;

            case 'f':
                $magentoCustomer->setGender('2');
                break;

            default:
                $magentoCustomer->setGender('3');
        }

        // 'Translate' the date of birth code from Gigya to Magento value
        $birthDay = $gigyaProfile->getBirthDay();
        $birthMonth = $gigyaProfile->getBirthMonth();
        $birthYear = $gigyaProfile->getBirthYear();

        if ($birthDay && $birthMonth && $birthYear) {
            $magentoCustomer->setDob(
                sprintf(
                    '%s-%s-%s',
                    $birthYear,
                    str_pad($birthMonth, 2, '0', STR_PAD_LEFT),
                    str_pad($birthDay, 2, '0', STR_PAD_LEFT)
                )
            );
        }

        // 'Translate' the subscribe boolean code from Gigya to Magento value
        $customerData = $gigyaUser->getData('subscribe');
        if (isset($customerData['subscribe'])) {
            if ($customerData['subscribe'] === 'false') {
                $gigyaUser->setData(array_merge($customerData, ['subscribe' => 0, 'data' => ['subscribe' => 0]]));
            }
            if ($customerData['subscribe'] === 'true') {
                $gigyaUser->setData(array_merge($customerData, ['subscribe' => 1, 'data' => ['subscribe' => 1]]));
            }
        }

        // Test kids
        $gigyaData = $gigyaUser->getData();
        if (array_key_exists('kids', $gigyaData)) {
            $kidValue = $gigyaData['kids'];
            if ($kidValue === 'Kid1') {
                $magentoCustomer->setData('gigya_first_kid', 'Hello!');
            }
        }

        // Test subscription
        $subscription = $gigyaUser->getSubscriptionById('june11th_01');
        if ($subscription) {
            $food = array('hpouhf');
            $magentoCustomer->setData('gigya_pets_nickname', json_encode($food));
        }
    }
}

 

Default Gigya Sync Field Mapping

Code Block
languagephp
themeMidnight
<?php
/**
 * Copyright
© 2016 X2i.
 */

namespace Gigya\GigyaIM\Observer;

use Gigya\CmsStarterKit\user\GigyaSubscription;
use Gigya\CmsStarterKit\user\GigyaSubscriptionContainer;
use Gigya\CmsStarterKit\user\GigyaUser;
use Gigya\CmsStarterKit\user\GigyaProfile;
use Magento\Customer\Model\Data\Customer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

/**
 * DefaultCMSSyncFieldMapping
 *
 * Default cms2g field mapping implementation. For now only attributes gender and date of birth (BirthDay BirthMonth BirthYear)
 *
 * To be effective one have to declare this observer on event 'pre_sync_to_gigya'.
 *
 */
class DefaultGigyaSyncFieldMapping implements ObserverInterface
{
    /**
     * Method execute
     *
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        /** @var GigyaUser $gigyaUser */
        $gigyaUser = $observer->getData('gigya_user');

        /** @var Customer $magentoCustomer */
        $magentoCustomer = $observer->getData('customer');
        /** @var GigyaProfile $gigyaProfile */
        $gigyaProfile = $gigyaUser->getProfile();

        // 'Translate' the gender code from Magento to Gigya value
        switch($magentoCustomer->getGender()) {
            case 1:
                $gigyaProfile->setGender('m');
                break;

            case 2:
                $gigyaProfile->setGender('f');
                break;

            default:
                $gigyaProfile->setGender('u');
        }

        // 'Translate' the date of birth code from Gigya to Magento value
        $dob = $magentoCustomer->getDob();

        if ($dob !== null && trim($dob) !== '') {
            $date = new \Zend_Date($dob, 'YYYY-MM-dd');
            $birthYear = (int)$date->get(\Zend_Date::YEAR);
            $birthMonth = (int)$date->get(\Zend_Date::MONTH);
            $birthDay = (int)$date->get(\Zend_Date::DAY);

            $gigyaProfile->setBirthDay($birthDay);
            $gigyaProfile->setBirthMonth($birthMonth);
            $gigyaProfile->setBirthYear($birthYear);
        }

        // Test kids
        $kidValue = $magentoCustomer->getCustomAttribute('gigya_first_kid')->getValue();
        if ($kidValue === 'Kid1') {
            $gigyaUser->setData(array('kids' => 'Kid2'));
        }

        // Test subscription
        $subscription = new GigyaSubscription(null);
        $subscription->setTags('["dsofuhdspu","dsfhdf","gqsfdqsf","qdfdsfdqsf"]');
        $gigyaUser->addSubscription('june11th_01', $subscription);
    }
}

 

 

Show If
groupgigya

Include Page
Template Internal Notice Box 3 Do Not Share Basic
Template Internal Notice Box 3 Do Not Share Basic

How to Add Gigya Custom Fields

Magento offers some customer attributes that do not have matching data fields in Gigya.

To create a mapping for these attributes:

  1. Create the custom data field in Gigya.
  2. If required, add it to the registration and/or edit profile form.
  3. Add a line to the gigyaSetCustomerFields method in the following format:

    Code Block
    languagephp
    linenumbersfalse
    $customer-><MAGENTO METHOD NAME>($gigya_user_account["data"]["<GIGYA FIELD NAME>"]);

    For example, Magento offers Prefix and Middle Name attributes for the customer, but Gigya does not. To map these attributes, first create them in Gigya and then map as follows:

    Code Block
    languagephp
    linenumbersfalse
    $customer->setPrefix($gigya_user_account["data"]["prefix"]);
    $customer->setMiddlename($gigya_user_account["data"]["middlename"]);

How to Add Magento Custom Fields

There are also Gigya user fields that do not have matching customer attributes in Magento by default.

To map Gigya fields to existing Magento custom fields:

  1. Set the fields using one of the following Magento methods, defined in /lib/internal/Magento/Framework/Api/AbstractExtensibleObject:

    Code Block
    languagephp
    linenumbersfalse
    $customer->setCustomAttribute($attributeCode, $attributeValue);
    $customer->setCustomAttributes(array());
  2. Add the fields to the profile update form by adding them as input fields in:  {Gigya Extension folder}/view/frontend/templates/gigya_script, in the gigyaUpdateProfile method. For example, to add the "prefix" field, add:

    Code Block
    languagejs
    linenumbersfalse
    gigyaMage2.Functions.createInputField("prefix", eventObj.data.prefix , form); 
Tip

In Magento Enterprise Edition, you can add custom attributes to customers via the Magento admin interface, by selecting Stores > Attributes:Customer > Add New Attribute.

Include Page
Template Internal Notice Box 5 Footer End Of Internal Content
Template Internal Notice Box 5 Footer End Of Internal Content

 

...