Skip to content

Commit

Permalink
Merge pull request #77 from jorgeeurekalabs/main
Browse files Browse the repository at this point in the history
v2.1.21
  • Loading branch information
jorgeeurekalabs authored May 27, 2024
2 parents 27d7aaf + 1cd8c14 commit 6cae92d
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Core/Logger/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ class Handler extends \Magento\Framework\Logger\Handler\Base
*
* @var string
*/
protected $fileName = '/var/log/activecampaign.log';
protected $fileName = 'var/log/activecampaign.log';
}
2 changes: 1 addition & 1 deletion Core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"config": {
"sort-packages": true
},
"version": "2.1.3",
"version": "2.1.4",
"require": {
"php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2"
},
Expand Down
2 changes: 1 addition & 1 deletion Core/etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="ActiveCampaign_Core" setup_version="2.1.3"/>
<module name="ActiveCampaign_Core" setup_version="2.1.4"/>
</config>
27 changes: 15 additions & 12 deletions Customer/Model/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,20 +289,22 @@ public function createGuestCustomer($data, $storeId){
$acContact = $this->createGuestContact($data);

$acCustomer = $this->searchCustomer($data['email'], $this->coreHelper->getConnectionId($storeId));
$ecomCustomerData=[];
$data['connectionid'] = $this->coreHelper->getConnectionId($storeId);
$data['externalid'] = $data['email'];
$data['acceptsMarketing'] = (int)$this->subscriberFactory->create()->loadBySubscriberEmail($data['email'],$this->storeManager->getStore()->getWebsiteId())->isSubscribed();
$ecomCustomerData['ecomCustomer'] = $data;
if (!$acCustomer) {
$ecomCustomerData=[];
$data['connectionid'] = $this->coreHelper->getConnectionId($storeId);
$data['externalid'] = $data['email'];
$data['acceptsMarketing'] = (int)$this->subscriberFactory->create()->loadBySubscriberEmail($data['email'],$this->storeManager->getStore()->getWebsiteId())->isSubscribed();
$ecomCustomerData['ecomCustomer'] = $data;

$result = $this->curl->createContacts(self::METHOD, self::ECOM_CUSTOMER_ENDPOINT, $ecomCustomerData);
if (!$result['success'] && $result['status'] == "404") {
$acCustomer = NULL;
}
if ($result['success'] && isset($result['data']['ecomCustomer']['id'])) {
$acCustomer = $result['data']['ecomCustomer']['id'];
}
}else{
$result = $this->curl->createContacts(self::METHOD_PUT, self::ECOM_CUSTOMER_ENDPOINT. '/' . $acCustomer, $ecomCustomerData);

}
if (!$result['success'] && $result['status'] == "404") {
$acCustomer = NULL;
}
if ($result['success'] && isset($result['data']['ecomCustomer']['id'])) {
$acCustomer = $result['data']['ecomCustomer']['id'];
}
}
return ['ac_contact_id' => $acContact, 'ac_customer_id' => $acCustomer];
Expand Down Expand Up @@ -411,3 +413,4 @@ public function syncCustomers(){
}

}

2 changes: 1 addition & 1 deletion Customer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"config": {
"sort-packages": true
},
"version": "2.1.9",
"version": "2.1.10",
"require": {
"php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2",
"activecampaign/core": "2.1.*"
Expand Down
2 changes: 1 addition & 1 deletion Customer/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<cron_day>*</cron_day>
<cron_month>*</cron_month>
<cron_weekday>*</cron_weekday>
<last_customers_updated>0</last_customers_updated>
<last_customers_updated>0000:00:00 00:00:00</last_customers_updated>
</customer>
</active_campaign>
</default>
Expand Down
2 changes: 1 addition & 1 deletion Customer/etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="ActiveCampaign_Customer" setup_version="2.1.9">
<module name="ActiveCampaign_Customer" setup_version="2.1.10">
<sequence>
<module name="ActiveCampaign_Core"/>
</sequence>
Expand Down
28 changes: 24 additions & 4 deletions Newsletter/Cron/NewsletterSyncCron.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ActiveCampaign\Customer\Model\Customer;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
use function Safe\strtotime;

class NewsletterSyncCron
{
Expand Down Expand Up @@ -69,6 +70,8 @@ class NewsletterSyncCron
*/
protected $customerResource;

protected $orderCollectionFactory;

/**
* NewsletterSyncCron constructor.
* @param Collection $newsletterCollection
Expand All @@ -79,6 +82,8 @@ class NewsletterSyncCron
* @param LoggerInterface $logger
* @param Customer $custoner
* @param CustomerFactory $customerFactory
* @param CustomerResource $customerResource
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
*/
public function __construct(
Collection $newsletterCollection,
Expand All @@ -89,9 +94,11 @@ public function __construct(
LoggerInterface $logger,
Customer $custoner,
CustomerFactory $customerFactory,
CustomerResource $customerResource
CustomerResource $customerResource,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
)
{
$this->orderCollectionFactory = $orderCollectionFactory;
$this->newsletterCollection = $newsletterCollection;
$this->activeCampaignHelper = $activeCampaignHelper;
$this->state = $state;
Expand All @@ -112,22 +119,26 @@ public function execute(): void
$isEnabled = $this->activeCampaignHelper->isNewslettersSyncEnabled();

if ($isEnabled) {
$date = $this->activeCampaignHelper->getLastSync();

$newsletterSyncNum = $this->activeCampaignHelper->getNewsletterSyncNum();
$newsletterCollection = $this->newsletterCollection
->addFieldToSelect('*')
->addFieldToFilter(
'ac_newsletter_sync_status',
['neq' => 1]
'change_status_at',
['gt' => new \Zend_Db_Expr('FROM_UNIXTIME('.$date.')')]
)
->addFieldToFilter(
'customer_id',
['eq' => 0]
)
->setOrder('change_status_at', 'desc')
->setPageSize($newsletterSyncNum)
->setCurPage(1);

foreach ($newsletterCollection as $news) {
$acContact=NULL;

try {
$contactData = [
'email' => $news->getSubscriberEmail()
Expand All @@ -136,7 +147,15 @@ public function execute(): void
$result = $this->customer->updateCustomer($this->getCustomer($news->getCustomerId()));
$acContact = $result['ac_contact_id'];
}else{
$acContact = $this->customer->createGuestContact($contactData);
$orders = $this->orderCollectionFactory->create()->addAttributeToFilter('customer_email', $news->getEmail())->setOrder('entity_id','desc');


if($orders->getSize()>0){
$store = $orders->getFirstItem()->getStoreId();
$this->customer->createGuestCustomer($contactData,$store);
}else{
$acContact = $this->customer->createGuestContact($contactData);
}

}

Expand All @@ -149,6 +168,7 @@ public function execute(): void
$this->logger->error('MODULE Order: ' . $e->getMessage());
}
}
$this->activeCampaignHelper->setLastSync(strtotime(date('Y-m-d H:i:s')));
}
} catch (\Exception $e) {
$this->logger->error('MODULE Order: ' . $e->getMessage());
Expand Down
40 changes: 39 additions & 1 deletion Newsletter/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ActiveCampaign\Newsletter\Helper;

use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;
Expand All @@ -10,22 +11,40 @@ class Data extends AbstractHelper
{
const ACTIVE_CAMPAIGN_NEWSLETTER_STATUS = "active_campaign/newsletter_sync/newsletter_sync_enable";
const ACTIVE_CAMPAIGN_NEWSLETTER_SYNC_NUM = "active_campaign/newsletter_sync/newsletter_sync_num";
const ACTIVE_CAMPAING_NEWSLETTER_LAST_SYNC = "active_campaign/newsletter_sync/last_newsletter_updated";

/**
* @var \Magento\Framework\App\State *
*/
private $state;

/**
* @var \Magento\Framework\App\Config\ConfigResource\ConfigInterface
*/
private $configInterface;

/**
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $cacheTypeList;

/**
* Data constructor.
* @param Context $context
* @param \Magento\Framework\App\State $state
* @param ConfigInterface $configInterface
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
*/
public function __construct(
Context $context,
\Magento\Framework\App\State $state
\Magento\Framework\App\State $state,
ConfigInterface $configInterface,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
)
{
parent::__construct($context);
$this->configInterface = $configInterface;
$this->cacheTypeList = $cacheTypeList;
$this->state = $state;
}

Expand All @@ -42,6 +61,25 @@ public function isNewslettersSyncEnabled($scopeCode = null)
);
}

/**
* @param null $scopeCode
* @return bool
*/
public function getLastSync($scopeCode = null)
{
return $this->scopeConfig->getValue(
self::ACTIVE_CAMPAING_NEWSLETTER_LAST_SYNC,
ScopeInterface::SCOPE_STORES,
$scopeCode
);
}

public function setLastSync($date, $scopeCode = null)
{
$scope = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
$this->configInterface->saveConfig(self::ACTIVE_CAMPAING_NEWSLETTER_LAST_SYNC, $date, $scope);
$this->cacheTypeList->invalidate(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
}

/**
* @param null $scopeCode
Expand Down
2 changes: 1 addition & 1 deletion Newsletter/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"config": {
"sort-packages": true
},
"version": "1.0.3",
"version": "1.0.4",
"require": {
"php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2",
"activecampaign/core": "2.1.*"
Expand Down
1 change: 1 addition & 0 deletions Newsletter/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<cron_day>*</cron_day>
<cron_month>*</cron_month>
<cron_weekday>*</cron_weekday>
<last_newsletter_updated>0</last_newsletter_updated>
</newsletter_sync>
</active_campaign>
</default>
Expand Down
2 changes: 1 addition & 1 deletion Newsletter/etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="ActiveCampaign_Newsletter" setup_version="1.0.3">
<module name="ActiveCampaign_Newsletter" setup_version="1.0.4">
<sequence>
<module name="ActiveCampaign_Core"/>
<module name="ActiveCampaign_Customer"/>
Expand Down
2 changes: 1 addition & 1 deletion marketplace-composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "activecampaign/module-integration",
"description": "ActiveCampaign extension for Magento 2.3 and 2.4",
"type": "metapackage",
"version": "2.1.20",
"version": "2.1.21",
"license": [
"OSL-3.0"
],
Expand Down

0 comments on commit 6cae92d

Please sign in to comment.