Skip to main content

Adding custom data attributes

To store custom attributes for a contact in Engage, these plugins can be used:

magento/app/code/Voyado/ExtendedVoyado/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
	<type name="Voyado\Magento2\Helper\ContactAttributesHelper">
		<plugin name="Voyado_ExtendedVoyado_Plugin_Sdk_To_Voyado"type="Voyado\ExtendedVoyado\Plugin\Sdk\ToVoyado"/>
	</type>
</config>

magento/app/code/Voyado/ExtendedVoyado/Plugin/Sdk/ToVoyado.php

<?php
namespace Voyado\ExtendedVoyado\Plugin\Sdk;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Model\Customer;
use Voyado\Magento2\Helper\ContactAttributesHelper as VoyadoContactAttributesHelper;

class ToVoyado
{
    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGetRequiredFieldsCustomer(
        VoyadoContactAttributesHelper $object,
        array $requiredFieldsCustomer
    ): array {
        return array_merge($requiredFieldsCustomer, ['married', 'region']);
    }

    /**
     * @param CustomerInterface|Customer $customer
     * @return \Swagger\Client\model\ContactAttributes
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGetContactAttributeDataFromCustomer(
        VoyadoContactAttributesHelper $object,
        array $result,
        $customer
    ): array {
        $extraData = [
            'married' => 'MarriedExtraPropertyDummyValue',
            'region' => 'RegionExtraPropertyDummyValue',
        ];
        return array_merge($result, $extraData);
    }
}