Skip to content

Commit c9ddbbb

Browse files
Merge pull request #88 from jorgeeurekalabs/main
Customer Custom Field Mapping
2 parents 9039a37 + 89a78fa commit c9ddbbb

File tree

13 files changed

+241
-11
lines changed

13 files changed

+241
-11
lines changed

AbandonedCart/Model/AbandonedCartSendData.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function sendAbandonedCartData($quoteId = null): array
253253

254254
$quote = $this->quoteRepository->get($abandonedCart->getEntityId());
255255
$AcCustomer = NULL;
256-
if ($this->isGuest($quote) || ($abandonedCart->getCustomerId() && !$this->getCustomer($abandonedCart->getCustomerId())->getId())) {
256+
if ($this->isGuest($quote) || ($abandonedCart->getCustomerId() && (!$this->getCustomer($abandonedCart->getCustomerId())->getId() || !$this->getCustomer($abandonedCart->getCustomerId())->getEmail() ))) {
257257
$customerEmail = $quote->getBillingAddress()->getEmail();
258258
if (!$customerEmail) {
259259
$result['error'] = __('Customer Email does not exist.');

AbandonedCart/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"config": {
1010
"sort-packages": true
1111
},
12-
"version": "2.1.14",
12+
"version": "2.1.15",
1313
"require": {
1414
"php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2",
1515
"activecampaign/core": "2.1.*"

AbandonedCart/etc/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="ActiveCampaign_AbandonedCart" setup_version="2.1.14">
9+
<module name="ActiveCampaign_AbandonedCart" setup_version="2.1.15">
1010
<sequence>
1111
<module name="Magento_Product"/>
1212
<module name="Magento_Sales"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
namespace ActiveCampaign\Customer\Block\Adminhtml\System\Config\Form\Field;
5+
6+
use Magento\Framework\View\Element\Html\Select;
7+
8+
class AcOptionColumn extends Select
9+
{
10+
private $curl;
11+
private $acHelper;
12+
public function __construct(
13+
\Magento\Framework\View\Element\Context $context,
14+
\ActiveCampaign\Core\Helper\Curl $curl,
15+
\ActiveCampaign\Core\Helper\Data $acHelper,
16+
array $data = []
17+
) {
18+
parent::__construct($context,$data);
19+
$this->curl = $curl;
20+
$this->acHelper = $acHelper;
21+
}
22+
public function setInputName($value)
23+
{
24+
return $this->setName($value);
25+
}
26+
27+
public function setInputId($value)
28+
{
29+
return $this->setId($value);
30+
31+
}
32+
public function _toHtml(): string
33+
{
34+
if (!$this->getOptions()) {
35+
$this->setOptions($this->getSourceOptions());
36+
}
37+
return parent::_toHtml();
38+
}
39+
40+
private function getSourceOptions(): array
41+
{
42+
$data = [];
43+
$fields=[];
44+
if($this->acHelper->isEnabled() && $this->acHelper->getConnectionId()){
45+
$data = $this->curl->createConnection('GET','/fields',[],[]);
46+
}
47+
if(count($data) && isset($data['data']) && isset($data['data']['fields']) && count($data['data']['fields'])){
48+
$data = $data['data'];
49+
foreach ($data['fields'] as $opt){
50+
$fields[]=['label' => $opt['title'], 'value'=> $opt['id']];
51+
}
52+
}
53+
return $fields;
54+
55+
}
56+
57+
protected function getCustomerAtt()
58+
{
59+
$ret = [];
60+
$collection = $this->attrCollection->create();
61+
62+
foreach ($collection as $item) {
63+
$ret[] = ['label' => $item->getFrontendLabel(), 'value' => $item->getId() ];
64+
}
65+
66+
67+
return $ret;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace ActiveCampaign\Customer\Block\Adminhtml\System\Config\Form\Field;
4+
5+
use ActiveCampaign\Customer\Block\Adminhtml\System\Config\Form\Field\CustomerOptionColumn;
6+
7+
class CustomerCustomFields extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
8+
{
9+
private $customerOptions;
10+
private $acOptions;
11+
12+
protected function _prepareArrayRow(\Magento\Framework\DataObject $row): void
13+
{
14+
$options = [];
15+
16+
$row->setData('option_extra_attrs', $options);
17+
}
18+
19+
20+
protected function _prepareToRender()
21+
{
22+
$this->addColumn(
23+
'ac_customer_field_id',
24+
['label' => __('Active Campaign'),
25+
'renderer' => $this->getAcField()]
26+
);
27+
$this->addColumn(
28+
'customer_field_id',
29+
[ 'label' => __('Magento'),
30+
'renderer' => $this->getCustomerField()
31+
]
32+
);
33+
$this->_addAfter = false;
34+
$this->_addButtonLabel = __('Add');
35+
}
36+
37+
private function getCustomerField(){
38+
if (!$this->customerOptions) {
39+
$this->customerOptions = $this->getLayout()->createBlock(
40+
CustomerOptionColumn::class,
41+
'',
42+
['data' => ['is_render_to_js_template' => true]]
43+
);
44+
}
45+
return $this->customerOptions;
46+
}
47+
48+
private function getAcField(){
49+
if (!$this->acOptions) {
50+
$this->acOptions = $this->getLayout()->createBlock(
51+
AcOptionColumn::class,
52+
'',
53+
['data' => ['is_render_to_js_template' => true]]
54+
);
55+
}
56+
return $this->acOptions;
57+
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
namespace ActiveCampaign\Customer\Block\Adminhtml\System\Config\Form\Field;
5+
6+
use Magento\Framework\View\Element\Html\Select;
7+
8+
class CustomerOptionColumn extends Select
9+
{
10+
/**
11+
* @var \Magento\Customer\Model\ResourceModel\Attribute\CollectionFactory
12+
*/
13+
private $attrCollection;
14+
15+
public function __construct(
16+
\Magento\Framework\View\Element\Context $context,
17+
\Magento\Customer\Model\ResourceModel\Attribute\CollectionFactory $collectionFactory,
18+
array $data = []
19+
) {
20+
parent::__construct($context,$data);
21+
$this->attrCollection = $collectionFactory;
22+
}
23+
public function setInputName($value)
24+
{
25+
return $this->setName($value);
26+
}
27+
28+
public function setInputId($value)
29+
{
30+
return $this->setId($value);
31+
32+
}
33+
public function _toHtml(): string
34+
{
35+
if (!$this->getOptions()) {
36+
$this->setOptions($this->getSourceOptions());
37+
}
38+
return parent::_toHtml();
39+
}
40+
41+
private function getSourceOptions(): array
42+
{
43+
$addressFields = [
44+
['label' => 'Default Shipping ZIP', 'value' => 'shipping__postcode'],
45+
['label' => 'Default Shipping City', 'value' => 'shipping__city'],
46+
['label' => 'Default Shipping Telephone', 'value' => 'shipping__telephone'],
47+
['label' => 'Default Shipping Region', 'value' => 'shipping__region'],
48+
['label' => 'Default Shipping Company', 'value' => 'shipping__company'],
49+
['label' => 'Default Billing ZIP', 'value' => 'billing__postcode'],
50+
['label' => 'Default Billing City', 'value' => 'billing__city'],
51+
['label' => 'Default Billing Telephone', 'value' => 'billing__telephone'],
52+
['label' => 'Default Billing Region', 'value' => 'billing__region'],
53+
['label' => 'Default Billing Company', 'value' => 'billing__company'],
54+
];
55+
return array_merge($addressFields, $this->getCustomerAtt());
56+
57+
}
58+
59+
protected function getCustomerAtt()
60+
{
61+
$ret = [];
62+
$collection = $this->attrCollection->create();
63+
64+
foreach ($collection as $item) {
65+
$ret[] = ['label' => $item->getFrontendLabel(), 'value' => $item->getAttributeCode() ];
66+
}
67+
68+
69+
return $ret;
70+
}
71+
}

Customer/Helper/Data.php

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Data extends AbstractHelper
1111
const ACTIVE_CAMPAIGN_CUSTOMER_SYNC = "active_campaign/customer/sync";
1212
const ACTIVE_CAMPAIGN_CUSTOMER_NUMBER_OF_CUSTOMERS = "active_campaign/customer/number_of_customers";
1313
const ACTIVE_CAMPAIGN_CUSTOMER_UPDATE_LAST_SYNC = "active_campaign/customer/last_customers_updated";
14+
const ACTIVE_CAMPAIGN_CUSTOMER_MAP_CUSTOM_FIELDS = "active_campaign/customer/map_custom_fields";
1415

1516
/**
1617
* @var \Magento\Framework\App\Config\ConfigResource\ConfigInterface
@@ -78,6 +79,15 @@ public function getLastCustomerUpdateSync($scopeCode = null)
7879
);
7980
}
8081

82+
public function getMapCustomFields($scopeCode = null)
83+
{
84+
return $this->scopeConfig->getValue(
85+
self::ACTIVE_CAMPAIGN_CUSTOMER_MAP_CUSTOM_FIELDS,
86+
ScopeInterface::SCOPE_STORES,
87+
$scopeCode
88+
);
89+
}
90+
8191
public function setLastCustomerUpdateSync($date, $scopeCode = null)
8292
{
8393
$scope = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT;

Customer/Model/Customer.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,27 @@ private function getTelephone($billingId = null)
195195
public function getFieldValues($customer)
196196
{
197197
$fieldValues = [];
198-
$customAttributes = $customer->getCustomAttributes();
198+
$customAttributes = $this->customerHelper->getMapCustomFields();
199199
if (!empty($customAttributes)) {
200-
foreach ($customAttributes as $attribute) {
201-
$attributeId = $this->eavAttribute->getIdByCode(MageCustomer::ENTITY, $attribute->getAttributeCode());
202-
$attributeValues['field'] = $attributeId;
203-
$attributeValues['value'] = $attribute->getValue();
200+
foreach (json_decode($customAttributes) as $attribute) {
201+
$attributeValues=[];
202+
$attributeValues['field'] = $attribute->ac_customer_field_id;
203+
$attributeValues['value'] ='';
204+
if(strncmp($attribute->customer_field_id,'shipping__',10) === 0 ){
205+
if($customer->getDefaultShippingAddress()){
206+
$attributeValues['value'] = $customer->getDefaultShippingAddress()->getData(substr($attribute->customer_field_id,10));
207+
}
208+
}elseif (strncmp($attribute->customer_field_id,'billing__',9) === 0){
209+
if($customer->getDefaultBillingAddress()){
210+
$attributeValues['value'] = $customer->getDefaultBillingAddress()->getData(substr($attribute->customer_field_id,9));
211+
}
212+
}else{
213+
if($attr = $customer->getResource()->getAttribute($attribute->customer_field_id)){
214+
$attributeValues['value'] = $attr->getFrontend()->getValue($customer);
215+
}else {
216+
$attributeValues['value'] = $customer->getData($attribute->customer_field_id);
217+
}
218+
}
204219
$fieldValues[] = $attributeValues;
205220
}
206221
}

Customer/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"config": {
1010
"sort-packages": true
1111
},
12-
"version": "2.1.13",
12+
"version": "2.1.14",
1313
"require": {
1414
"php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2",
1515
"activecampaign/core": "2.1.*"

Customer/etc/adminhtml/system.xml

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
<field id="sync">1</field>
5050
</depends>
5151
</field>
52+
<field id="map_custom_fields" translate="Map custom fields" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="1">
53+
<label>Mapping Customer Custom Fields</label>
54+
<frontend_model>ActiveCampaign\Customer\Block\Adminhtml\System\Config\Form\Field\CustomerCustomFields</frontend_model>
55+
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
56+
</field>
5257
<field id="number_of_customers" translate="label comment" type="text" sortOrder="30" showInDefault="1">
5358
<label>Number of customers</label>
5459
<validate>required-entry validate-number validate-greater-than-zero validate-integer</validate>

Customer/etc/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="ActiveCampaign_Customer" setup_version="2.1.13">
3+
<module name="ActiveCampaign_Customer" setup_version="2.1.14">
44
<sequence>
55
<module name="ActiveCampaign_Core"/>
66
</sequence>

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "activecampaign/module-integration",
33
"description": "ActiveCampaign extension for Magento 2.3 and 2.4",
44
"type": "magento2-component",
5+
"version": "2.1.24",
56
"license": "OSL-3.0",
67
"require": {
78
"php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2"

marketplace-composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "activecampaign/module-integration",
33
"description": "ActiveCampaign extension for Magento 2.3 and 2.4",
44
"type": "metapackage",
5-
"version": "2.1.23",
5+
"version": "2.1.24",
66
"license": [
77
"OSL-3.0"
88
],

0 commit comments

Comments
 (0)