Skip to content

Commit

Permalink
fix the campaign id taken it from the activity API #1866 for 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Feb 16, 2024
1 parent 0e2e6ca commit e16b294
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion Model/Api/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Order
protected $_counter;

protected $_batchId;
protected $modifiedOrder = false;

/**
* @param \Ebizmarts\MailChimp\Helper\Data $helper
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit e16b294

Please sign in to comment.