diff --git a/Core/Logger/Handler.php b/Core/Logger/Handler.php index 981e905..c31696f 100644 --- a/Core/Logger/Handler.php +++ b/Core/Logger/Handler.php @@ -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'; } diff --git a/Core/composer.json b/Core/composer.json index 10afd19..eaa24a1 100644 --- a/Core/composer.json +++ b/Core/composer.json @@ -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" }, diff --git a/Core/etc/module.xml b/Core/etc/module.xml index ebc9a16..5e43007 100644 --- a/Core/etc/module.xml +++ b/Core/etc/module.xml @@ -1,4 +1,4 @@ - + diff --git a/Customer/Model/Customer.php b/Customer/Model/Customer.php index be11e98..a12f83a 100644 --- a/Customer/Model/Customer.php +++ b/Customer/Model/Customer.php @@ -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]; @@ -411,3 +413,4 @@ public function syncCustomers(){ } } + diff --git a/Customer/composer.json b/Customer/composer.json index 35c354b..816edde 100644 --- a/Customer/composer.json +++ b/Customer/composer.json @@ -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.*" diff --git a/Customer/etc/config.xml b/Customer/etc/config.xml index 0ccfa46..1dc3350 100644 --- a/Customer/etc/config.xml +++ b/Customer/etc/config.xml @@ -16,7 +16,7 @@ * * * - 0 + 0000:00:00 00:00:00 diff --git a/Customer/etc/module.xml b/Customer/etc/module.xml index 9cad0f7..1942019 100644 --- a/Customer/etc/module.xml +++ b/Customer/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/Newsletter/Cron/NewsletterSyncCron.php b/Newsletter/Cron/NewsletterSyncCron.php index c15f020..b64bf1d 100644 --- a/Newsletter/Cron/NewsletterSyncCron.php +++ b/Newsletter/Cron/NewsletterSyncCron.php @@ -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 { @@ -69,6 +70,8 @@ class NewsletterSyncCron */ protected $customerResource; + protected $orderCollectionFactory; + /** * NewsletterSyncCron constructor. * @param Collection $newsletterCollection @@ -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, @@ -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; @@ -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() @@ -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); + } } @@ -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()); diff --git a/Newsletter/Helper/Data.php b/Newsletter/Helper/Data.php index a36a937..4f6d245 100644 --- a/Newsletter/Helper/Data.php +++ b/Newsletter/Helper/Data.php @@ -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; @@ -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; } @@ -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 diff --git a/Newsletter/composer.json b/Newsletter/composer.json index d610f62..b1c227e 100644 --- a/Newsletter/composer.json +++ b/Newsletter/composer.json @@ -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.*" diff --git a/Newsletter/etc/config.xml b/Newsletter/etc/config.xml index 405535d..f05a6b1 100644 --- a/Newsletter/etc/config.xml +++ b/Newsletter/etc/config.xml @@ -11,6 +11,7 @@ * * * + 0 diff --git a/Newsletter/etc/module.xml b/Newsletter/etc/module.xml index 74bc7b3..36d0732 100644 --- a/Newsletter/etc/module.xml +++ b/Newsletter/etc/module.xml @@ -1,7 +1,7 @@ - + diff --git a/marketplace-composer.json b/marketplace-composer.json index 3f7406d..acd7cce 100644 --- a/marketplace-composer.json +++ b/marketplace-composer.json @@ -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" ],