diff --git a/Block/Adminhtml/System/Config/Form/Field/MailchimpMap.php b/Block/Adminhtml/System/Config/Form/Field/MailchimpMap.php index 677130d5..7ec37182 100755 --- a/Block/Adminhtml/System/Config/Form/Field/MailchimpMap.php +++ b/Block/Adminhtml/System/Config/Form/Field/MailchimpMap.php @@ -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()); diff --git a/Block/Adminhtml/System/Config/Form/Field/VarsMap.php b/Block/Adminhtml/System/Config/Form/Field/VarsMap.php index 1e022562..b3243e59 100644 --- a/Block/Adminhtml/System/Config/Form/Field/VarsMap.php +++ b/Block/Adminhtml/System/Config/Form/Field/VarsMap.php @@ -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() { @@ -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)); } } diff --git a/Cron/Webhook.php b/Cron/Webhook.php index 23cbcf11..653a9f7e 100644 --- a/Cron/Webhook.php +++ b/Cron/Webhook.php @@ -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']); } diff --git a/Helper/Data.php b/Helper/Data.php index f610ca5f..46e3d3be 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -206,8 +206,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper */ protected $_deploymentConfig; - private $customerAtt = null; + private $addressAtt = null; private $_mapFields = null; /** @@ -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 = []; @@ -370,6 +381,37 @@ 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; @@ -377,7 +419,7 @@ public function resetMapFields() 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); @@ -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() { @@ -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; } } @@ -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; }