From e16b294eb21d5513f4f35420c5928ebe68718db3 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 16 Feb 2024 16:46:31 -0300 Subject: [PATCH 01/11] fix the campaign id taken it from the activity API #1866 for 2.4 --- Model/Api/Order.php | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/Model/Api/Order.php b/Model/Api/Order.php index 31c20603..82649be4 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 @@ -205,6 +206,10 @@ protected function _getModifiedOrders($magentoStoreId) } $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId, true); + if ($this->modifiedOrder) { + $order->save(); + $this->modifiedOrder = false; + } if ($orderJson!==false) { if (!empty($orderJson)) { $this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::ORD_MOD); @@ -286,6 +291,10 @@ protected function _getNewOrders($magentoStoreId) } } $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId); + if ($this->modifiedOrder) { + $order->save(); + $this->modifiedOrder = false; + } if ($orderJson!==false) { if (!empty($orderJson)) { $this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::ORD_NEW); @@ -335,6 +344,13 @@ protected function generatePOSTPayload( $data['id'] = $order->getIncrementId(); if ($order->getMailchimpCampaignId()) { $data['campaign_id'] = $order->getMailchimpCampaignId(); + } else { + 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 +754,25 @@ protected function _getPromoData(\Magento\Sales\Model\Order $order) } return $promo; } - + protected function getCampaign($store, $email) + { + $campaign_id = null; + $api = $this->_helper->getApi($store); + try { + $activity = $api->lists->members->memberActivity->get($this->_helper->getDefaultList($store), md5($email), null, null); + if ($activity) { + foreach ($activity['activity'] as $act) { + if (key_exists('action', $act) && $act['action'] == 'click' && 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)) { From 514e642e5e72e6958fcfd310a2485b7db77ed9e5 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Mon, 19 Feb 2024 11:18:31 -0300 Subject: [PATCH 02/11] use click or open to retrieve the campaign #1866 for 2.4 --- Model/Api/Order.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Api/Order.php b/Model/Api/Order.php index 82649be4..077a4338 100644 --- a/Model/Api/Order.php +++ b/Model/Api/Order.php @@ -762,7 +762,7 @@ protected function getCampaign($store, $email) $activity = $api->lists->members->memberActivity->get($this->_helper->getDefaultList($store), md5($email), null, null); if ($activity) { foreach ($activity['activity'] as $act) { - if (key_exists('action', $act) && $act['action'] == 'click' && key_exists('campaign_id', $act) && $act['campaign_id']) { + if (key_exists('action', $act) && ($act['action'] == 'click' || $act['action'] == 'open')&& key_exists('campaign_id', $act) && $act['campaign_id']) { $campaign_id = $act['campaign_id']; break; } From 28140dffb2f78eec850e92e6ca79044c0da5b5b1 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 20 Feb 2024 16:18:32 -0300 Subject: [PATCH 03/11] need the 3.0.58 version of the lib #1866 for 2.4 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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": "*" } } From 78bad9837146860ab3aded5dce9d2038d2030020 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 20 Feb 2024 16:19:27 -0300 Subject: [PATCH 04/11] add a multiselect for campaign action #1866 for 2.4 --- etc/adminhtml/system.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 2e3f5b0f..e836864c 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -229,6 +229,16 @@ 1 + + + Ebizmarts\MailChimp\Model\Config\Source\CampaignAction + 1 + 3 + + 1 + + + Reset Errors and retry Ebizmarts\MailChimp\Block\Adminhtml\System\Config\ResetErrors From 8833796b1b75b7e7fb3df151c2952a8d78e7bba9 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 20 Feb 2024 16:19:56 -0300 Subject: [PATCH 05/11] add default values for campaign action multiselect #1866 for 2.4 --- etc/config.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 etc/config.xml 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 + + + + From 8b319c30c316b0b748e6fa0c12cb87331bbf4392 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 20 Feb 2024 16:20:24 -0300 Subject: [PATCH 06/11] add a multiselect for campaign action #1866 for 2.4 --- Model/Config/Source/CampaignAction.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Model/Config/Source/CampaignAction.php 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; + } +} From 127dfb6aa637b2de8ae8b3c1936755edca31676f Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 20 Feb 2024 16:20:59 -0300 Subject: [PATCH 07/11] change the size of campaign multiselect to 3 #1866 for 2.4 --- view/adminhtml/web/js/configapikey.js | 1 + 1 file changed, 1 insertion(+) 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 () { From ac37515fbee7a523892974028aec487def292efb Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 20 Feb 2024 16:22:23 -0300 Subject: [PATCH 08/11] ask for activity only when the store is synced #1866 for 2.4 --- Helper/Data.php | 1 + Model/Api/Order.php | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) 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 077a4338..e79cf9be 100644 --- a/Model/Api/Order.php +++ b/Model/Api/Order.php @@ -164,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]); @@ -205,7 +206,7 @@ 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; @@ -248,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]); @@ -290,7 +292,7 @@ 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; @@ -338,13 +340,14 @@ 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(); - } else { + } elseif ($isSynced) { if ($campaignId = $this->getCampaign($magentoStoreId, $order->getCustomerEmail())) { $data['campaign_id'] = $campaignId; $order->setMailchimpCampaignId($campaignId); @@ -758,8 +761,9 @@ 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); + $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) && ($act['action'] == 'click' || $act['action'] == 'open')&& key_exists('campaign_id', $act) && $act['campaign_id']) { From 5f69efd49a6bf642e5d539e370181a92b7f25357 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Tue, 20 Feb 2024 17:19:23 -0300 Subject: [PATCH 09/11] add comment to the campaign_action field #1866 for 2.4 --- etc/adminhtml/system.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index e836864c..497596c7 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -230,13 +230,14 @@ - + Ebizmarts\MailChimp\Model\Config\Source\CampaignAction 1 3 1 + Order attribution based on customer actions From 7a884575a7aa8488303655396fa4127c4886d9ea Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 22 Feb 2024 08:51:31 -0300 Subject: [PATCH 10/11] fix condition to take the action #1866 for 2.4 --- Model/Api/Order.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Model/Api/Order.php b/Model/Api/Order.php index e79cf9be..3fed751d 100644 --- a/Model/Api/Order.php +++ b/Model/Api/Order.php @@ -354,6 +354,8 @@ protected function generatePOSTPayload( $order->setMailchimpFlag(1); $this->modifiedOrder = true; } + } else { + $this->_helper->log(); } if ($order->getMailchimpLandingPage()) { @@ -766,7 +768,7 @@ protected function getCampaign($store, $email) $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) && ($act['action'] == 'click' || $act['action'] == 'open')&& key_exists('campaign_id', $act) && $act['campaign_id']) { + if (key_exists('action', $act) && key_exists('campaign_id', $act) && $act['campaign_id']) { $campaign_id = $act['campaign_id']; break; } From 2f2600cc6beb664e9890c8aa4bf687ddeb98a16f Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 22 Feb 2024 08:52:12 -0300 Subject: [PATCH 11/11] fix condition to take the action #1866 for 2.4 --- Model/Api/Order.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Model/Api/Order.php b/Model/Api/Order.php index 3fed751d..98c27a50 100644 --- a/Model/Api/Order.php +++ b/Model/Api/Order.php @@ -354,8 +354,6 @@ protected function generatePOSTPayload( $order->setMailchimpFlag(1); $this->modifiedOrder = true; } - } else { - $this->_helper->log(); } if ($order->getMailchimpLandingPage()) {