Skip to content

Commit

Permalink
Merge pull request #1071 from mailchimp/Issue1025-2.1
Browse files Browse the repository at this point in the history
closes #1025 for magento 2.1
  • Loading branch information
gonzaloebiz authored Oct 13, 2020
2 parents 94edb0a + 70a930b commit 8a087d9
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 28 deletions.
19 changes: 15 additions & 4 deletions Block/Adminhtml/System/Config/Form/Field/MailchimpMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,22 @@ protected function _getMailchimpTags()
$scope = 'default';
}

$api = $this->_helper->getApi($storeId,$scope);
$api = $this->_helper->getApi($storeId, $scope);
try {
$merge = $api->lists->mergeFields->getAll($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST, $storeId, $scope), null, null, \Ebizmarts\MailChimp\Helper\Data::MAX_MERGEFIELDS);
foreach ($merge['merge_fields'] as $item) {
$ret[$item['tag']] = $item['tag'] . ' (' . $item['name'] . ' : ' . $item['type'] . ')';
$merge = $api->lists->mergeFields->getAll(
$this->_helper->getConfigValue(
\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST,
$storeId,
$scope
),
null,
null,
\Ebizmarts\MailChimp\Helper\Data::MAX_MERGEFIELDS
);
if (is_array($merge) && key_exists('merge_fields', $merge)) {
foreach ($merge['merge_fields'] as $item) {
$ret[$item['tag']] = $item['tag'] . ' (' . $item['name'] . ' : ' . $item['type'] . ')';
}
}
} catch (\Mailchimp_Error $e) {
$this->_helper->log($e->getFriendlyMessage());
Expand Down
32 changes: 31 additions & 1 deletion Block/Adminhtml/System/Config/Form/Field/VarsMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ public function __construct(
parent::__construct($context, $data);
$this->_attCollection = $attCollection;
}
protected function _getAddressAtt()
{
$ret = [];
$ret['default_shipping##zip'] = __('Shipping Zip Code');
$ret['default_shipping##country'] = __('Shipping Country');
$ret['default_shipping##city'] = __('Shipping City');
$ret['default_shipping##state'] = __('Shipping State');
$ret['default_shipping##telephone'] = __('Shipping Telephone');

$ret['default_billing##zip'] = __('Billing Zip Code');
$ret['default_billing##country'] = __('Billing Country');
$ret['default_billing##city'] = __('Billing City');
$ret['default_billing##state'] = __('Billing State');
$ret['default_billing##telephone'] = __('Billing Telephone');

return $ret;
}

protected function _getBindableAttributes()
{
$systemAtt = $this->_getCustomerAtt();
$extraAtt = $this->_getAddressAtt();

// Note: We cannot use array_merge here because we need to hold
// numeric indexes as they are
$ret = $systemAtt + $extraAtt;

natsort($ret);
return $ret;
}

protected function _getCustomerAtt()
{
Expand Down Expand Up @@ -63,7 +93,7 @@ public function setInputName($value)
public function _toHtml()
{
if (!$this->getOptions()) {
foreach ($this->_getCustomerAtt() as $attId => $attLabel) {
foreach ($this->_getBindableAttributes() as $attId => $attLabel) {
$this->addOption($attId, addslashes($attLabel));
}
}
Expand Down
4 changes: 2 additions & 2 deletions Cron/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ protected function _processMerges(\Magento\Customer\Model\Customer $customer, $d
if (is_array($mapFields)) {
foreach ($mapFields as $map) {
if ($map['mailchimp'] == $key) {
if (!$map['isAddress']&&$map['customer_field'] != "dob") {
if (!$map['isAddress'] && $map['customer_field'] != "dob" && strpos($map['customer_field'], '##') !== false) {
if (count($map['options'])) {
foreach($map['options'] as $option) {
foreach ($map['options'] as $option) {
if ($option['label'] == $value) {
$customer->setData($map['customer_field'], $option['value']);
}
Expand Down
107 changes: 86 additions & 21 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
*/
protected $_deploymentConfig;


private $customerAtt = null;
private $addressAtt = null;
private $_mapFields = null;

/**
Expand Down Expand Up @@ -342,6 +342,17 @@ public function getApi($store = null, $scope = null)
$this->_api->setUserAgent('Mailchimp4Magento' . (string)$this->getModuleVersion());
return $this->_api;
}
private function getBindableAttributes()
{
$systemAtt = $this->getCustomerAtts();
$extraAtt = $this->getAddressAtt();

// Note: We cannot use array_merge here because we need to hold
// numeric indexes as they are
$ret = $systemAtt + $extraAtt;

return $ret;
}
private function getCustomerAtts()
{
$ret = [];
Expand Down Expand Up @@ -370,14 +381,45 @@ private function getCustomerAtts()
}
return $this->customerAtt;
}
private function getAddressAtt()
{
$ret = [];
if (!$this->addressAtt) {
$elements = [
'default_shipping##zip',
'default_shipping##country',
'default_shipping##city',
'default_shipping##state',
'default_shipping##telephone',
'default_billing##zip',
'default_billing##country',
'default_billing##city',
'default_billing##state',
'default_billing##telephone'
];

foreach($elements as $item) {
$ret[$item] = [
'attCode' => $item,
'isDate' => false,
'isAddress' => false,
'options' => []
];
}

$this->addressAtt = $ret;
}

return $this->addressAtt;
}
public function resetMapFields()
{
$this->_mapFields = null;
}
public function getMapFields($storeId = null)
{
if (!$this->_mapFields) {
$customerAtt = $this->getCustomerAtts();
$customerAtt = $this->getBindableAttributes();
$data = $this->getConfigValue(self::XML_MERGEVARS, $storeId);
try {
$data = $this->unserialize($data);
Expand Down Expand Up @@ -598,7 +640,11 @@ public function createStore($listId = null, $storeId)
}
public function getMCMinSyncDateFlag($storeId = null)
{
return $this->getConfigValue(self::XML_PATH_SYNC_DATE, $storeId);
$syncDate = $this->getConfigValue(self::XML_PATH_SYNC_DATE, $storeId);
if ($syncDate=='') {
$syncDate = '0000-00-00';
}
return $syncDate;
}
public function getBaseDir()
{
Expand All @@ -617,28 +663,44 @@ public function getMergeVars(\Magento\Customer\Model\Customer $customer, $storeI
$mapFields = $this->getMapFields($storeId);
if (is_array($mapFields)) {
foreach ($mapFields as $map) {
$value = $customer->getData($map['customer_field']);
if ($value) {
if ($map['isDate']) {
$format = $this->getDateFormat();
if ($map['customer_field'] == 'dob') {
$format = substr($format, 0, 3);
if (strpos($map['customer_field'], '##') !== false) {
$parts = explode('##', $map['customer_field']);
$attributeCode = $parts[0];
$fieldName = $parts[1];
$customerAddress = $customer->getPrimaryAddress($attributeCode);
if ($customerAddress !== false) {
$addressData = $this->_getAddressValues($customerAddress);
if (!empty($addressData[$fieldName])) {
$value = $addressData[$fieldName];
}
$value = date($format, strtotime($value));
} elseif ($map['isAddress']) {
$customerAddress = $customer->getPrimaryAddress($map['customer_field']);
$value = [];
if ($customerAddress !== false) {
$value = $this->_getAddressValues($customerAddress);
}
} elseif (count($map['options'])) {
foreach ($map['options'] as $option) {
if ($option['value'] == $value) {
$value = $option['label'];
break;
}
} else {
$value = $customer->getData($map['customer_field']);
if ($value) {
if ($map['isDate']) {
$format = $this->getDateFormat();
if ($map['customer_field'] == 'dob') {
$format = substr($format, 0, 3);
}
$value = date($format, strtotime($value));
} elseif ($map['isAddress']) {
$customerAddress = $customer->getPrimaryAddress($map['customer_field']);
$value = [];
if ($customerAddress !== false) {
$value = $this->_getAddressValues($customerAddress);
}
} elseif (count($map['options'])) {
foreach ($map['options'] as $option) {
if ($option['value'] == $value) {
$value = $option['label'];
break;
}
}
}
}
}

if (!empty($value)) {
$mergeVars[$map['mailchimp']] = $value;
}
}
Expand Down Expand Up @@ -680,6 +742,9 @@ private function _getAddressValues(\Magento\Customer\Model\Address\AbstractAddre
$country = $this->_countryInformation->getCountryInfo($address->getCountryId());
$addressData["country"] = $country->getFullNameLocale();
}
if ($address->getTelephone()) {
$addressData['telephone'] = $address->getTelephone();
}
}
return $addressData;
}
Expand Down

0 comments on commit 8a087d9

Please sign in to comment.