diff --git a/Helper/Data.php b/Helper/Data.php
index 00c9ebce..3ca47829 100755
--- a/Helper/Data.php
+++ b/Helper/Data.php
@@ -49,6 +49,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const XML_MAGENTO_MAIL = 'mailchimp/general/magentoemail';
const XML_SEND_PROMO = 'mailchimp/ecommerce/send_promo';
const XML_INCLUDING_TAXES = 'mailchimp/ecommerce/including_taxes';
+ const XML_CAMPAIGN_ACTION = 'mailchimp/ecommerce/campaign_action';
const XML_POPUP_FORM = 'mailchimp/general/popup_form';
const XML_POPUP_URL = 'mailchimp/general/popup_url';
const XML_CLEAN_ERROR_MONTHS = 'mailchimp/ecommerce/clean_errors_months';
diff --git a/Model/Api/Order.php b/Model/Api/Order.php
index 31c20603..98c27a50 100644
--- a/Model/Api/Order.php
+++ b/Model/Api/Order.php
@@ -85,6 +85,7 @@ class Order
protected $_counter;
protected $_batchId;
+ protected $modifiedOrder = false;
/**
* @param \Ebizmarts\MailChimp\Helper\Data $helper
@@ -163,6 +164,7 @@ protected function _getModifiedOrders($magentoStoreId)
\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,
$magentoStoreId
);
+ $isSynced = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_IS_SYNC, $magentoStoreId);
$modifiedOrders = $this->_getCollection();
// select orders for the current Magento store id
$modifiedOrders->addFieldToFilter('store_id', ['eq' => $magentoStoreId]);
@@ -204,7 +206,11 @@ protected function _getModifiedOrders($magentoStoreId)
}
}
- $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId, true);
+ $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId, true, $isSynced);
+ if ($this->modifiedOrder) {
+ $order->save();
+ $this->modifiedOrder = false;
+ }
if ($orderJson!==false) {
if (!empty($orderJson)) {
$this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::ORD_MOD);
@@ -243,6 +249,7 @@ protected function _getNewOrders($magentoStoreId)
\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,
$magentoStoreId
);
+ $isSynced = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_IS_SYNC, $magentoStoreId);
$newOrders = $this->_getCollection();
// select carts for the current Magento store id
$newOrders->addFieldToFilter('store_id', ['eq' => $magentoStoreId]);
@@ -285,7 +292,11 @@ protected function _getNewOrders($magentoStoreId)
$this->_counter++;
}
}
- $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId);
+ $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId, false, $isSynced);
+ if ($this->modifiedOrder) {
+ $order->save();
+ $this->modifiedOrder = false;
+ }
if ($orderJson!==false) {
if (!empty($orderJson)) {
$this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::ORD_NEW);
@@ -329,12 +340,20 @@ protected function generatePOSTPayload(
\Magento\Sales\Model\Order $order,
$mailchimpStoreId,
$magentoStoreId,
- $isModifiedOrder = false
+ $isModifiedOrder,
+ $isSynced
) {
$data = [];
$data['id'] = $order->getIncrementId();
if ($order->getMailchimpCampaignId()) {
$data['campaign_id'] = $order->getMailchimpCampaignId();
+ } elseif ($isSynced) {
+ if ($campaignId = $this->getCampaign($magentoStoreId, $order->getCustomerEmail())) {
+ $data['campaign_id'] = $campaignId;
+ $order->setMailchimpCampaignId($campaignId);
+ $order->setMailchimpFlag(1);
+ $this->modifiedOrder = true;
+ }
}
if ($order->getMailchimpLandingPage()) {
@@ -738,7 +757,26 @@ protected function _getPromoData(\Magento\Sales\Model\Order $order)
}
return $promo;
}
-
+ protected function getCampaign($store, $email)
+ {
+ $campaign_id = null;
+ $api = $this->_helper->getApi($store);
+ $actions = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_CAMPAIGN_ACTION, $store);
+ try {
+ $activity = $api->lists->members->memberActivity->get($this->_helper->getDefaultList($store), md5($email), null, null, $actions);
+ if ($activity) {
+ foreach ($activity['activity'] as $act) {
+ if (key_exists('action', $act) && key_exists('campaign_id', $act) && $act['campaign_id']) {
+ $campaign_id = $act['campaign_id'];
+ break;
+ }
+ }
+ }
+ } catch (\Mailchimp_Error $e) {
+ $this->_helper->log("No activity for $email");
+ }
+ return $campaign_id;
+ }
protected function _updateOrder($storeId, $entityId, $sync_delta = null, $sync_error = null, $sync_modified = null)
{
if (!empty($sync_error)) {
diff --git a/Model/Config/Source/CampaignAction.php b/Model/Config/Source/CampaignAction.php
new file mode 100644
index 00000000..c03ee626
--- /dev/null
+++ b/Model/Config/Source/CampaignAction.php
@@ -0,0 +1,16 @@
+ 'sent', 'label' => 'Sent'],
+ ['value' => 'open', 'label' => 'Open'],
+ ['value' => 'click', 'label' => 'Click']
+ ];
+ public function toOptionArray()
+ {
+ return $this->actions;
+ }
+}
diff --git a/composer.json b/composer.json
index 666ad190..4dcff35d 100644
--- a/composer.json
+++ b/composer.json
@@ -25,7 +25,7 @@
},
"require" : {
"magento/framework": "103.0.*",
- "ebizmarts/mailchimp-lib": ">=3.0.37",
+ "ebizmarts/mailchimp-lib": ">=3.0.38",
"ext-json": "*"
}
}
diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml
index 2e3f5b0f..497596c7 100644
--- a/etc/adminhtml/system.xml
+++ b/etc/adminhtml/system.xml
@@ -229,6 +229,17 @@
1
+
+
+ Ebizmarts\MailChimp\Model\Config\Source\CampaignAction
+ 1
+ 3
+
+ 1
+
+ Order attribution based on customer actions
+
+
Reset Errors and retry
Ebizmarts\MailChimp\Block\Adminhtml\System\Config\ResetErrors
diff --git a/etc/config.xml b/etc/config.xml
new file mode 100644
index 00000000..590d6640
--- /dev/null
+++ b/etc/config.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+ sent,open,click
+
+
+
+
diff --git a/view/adminhtml/web/js/configapikey.js b/view/adminhtml/web/js/configapikey.js
index c1870881..d4c8b214 100644
--- a/view/adminhtml/web/js/configapikey.js
+++ b/view/adminhtml/web/js/configapikey.js
@@ -85,6 +85,7 @@ define(
if (ecommerceEnabled == 0 && abandonedCartEnabled == 1) {
self._changeAbandonedCart();
}
+ $('#mailchimp_ecommerce_campaign_action').attr('size',3);
},
_changeEcommerce: function () {