From 9ef6dfb2415496553819b45c81d5c089cdbed93b Mon Sep 17 00:00:00 2001 From: Jorge Venzon Date: Wed, 9 Aug 2023 16:37:16 -0300 Subject: [PATCH 1/2] Added Newsletter sync --- .idea/.gitignore | 8 -- .../inspectionProfiles/profiles_settings.xml | 5 - .idea/magento2plugin.xml | 6 - .idea/module-integration.iml | 16 --- .idea/php.xml | 6 - .idea/vcs.xml | 6 - .../System/Config/SyncAbandonedCartData.php | 9 +- AbandonedCart/Model/AbandonedCartSendData.php | 63 ++++++--- .../Listing/AbandonedCartDataProvider.php | 2 +- AbandonedCart/composer.json | 2 +- AbandonedCart/etc/module.xml | 2 +- .../Adminhtml/System/Config/Connect.php | 20 ++- .../Adminhtml/System/Config/Disconnect.php | 12 +- Core/composer.json | 2 +- Core/etc/module.xml | 2 +- Customer/Cron/CustomerSync.php | 2 + Customer/Model/Customer.php | 1 + Customer/composer.json | 2 +- Customer/etc/module.xml | 2 +- .../System/Config/NewsletterSyncStatus.php | 119 +++++++++++++++++ .../Block/Adminhtml/System/Config/Widget.php | 27 ++++ Newsletter/Cron/NewsletterSyncCron.php | 126 ++++++++++++++++++ Newsletter/Helper/Data.php | 58 ++++++++ Newsletter/Model/Config/CronConfig.php | 113 ++++++++++++++++ Newsletter/composer.json | 25 ++++ Newsletter/etc/adminhtml/system.xml | 65 +++++++++ Newsletter/etc/config.xml | 17 +++ Newsletter/etc/crontab.xml | 10 ++ Newsletter/etc/db_schema.xml | 11 ++ Newsletter/etc/module.xml | 9 ++ Newsletter/registration.php | 9 ++ .../config/newsletter_sync_status.phtml | 52 ++++++++ composer.json | 11 +- marketplace-composer.json | 5 +- 34 files changed, 729 insertions(+), 96 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/magento2plugin.xml delete mode 100644 .idea/module-integration.iml delete mode 100644 .idea/php.xml delete mode 100644 .idea/vcs.xml create mode 100644 Newsletter/Block/Adminhtml/System/Config/NewsletterSyncStatus.php create mode 100644 Newsletter/Block/Adminhtml/System/Config/Widget.php create mode 100644 Newsletter/Cron/NewsletterSyncCron.php create mode 100644 Newsletter/Helper/Data.php create mode 100644 Newsletter/Model/Config/CronConfig.php create mode 100644 Newsletter/composer.json create mode 100644 Newsletter/etc/adminhtml/system.xml create mode 100644 Newsletter/etc/config.xml create mode 100644 Newsletter/etc/crontab.xml create mode 100644 Newsletter/etc/db_schema.xml create mode 100644 Newsletter/etc/module.xml create mode 100644 Newsletter/registration.php create mode 100644 Newsletter/view/adminhtml/templates/system/config/newsletter_sync_status.phtml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 0eefe32..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/magento2plugin.xml b/.idea/magento2plugin.xml deleted file mode 100644 index 6485e05..0000000 --- a/.idea/magento2plugin.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/module-integration.iml b/.idea/module-integration.iml deleted file mode 100644 index 8e1a72d..0000000 --- a/.idea/module-integration.iml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index 0087ae2..0000000 --- a/.idea/php.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/AbandonedCart/Block/System/Config/SyncAbandonedCartData.php b/AbandonedCart/Block/System/Config/SyncAbandonedCartData.php index b19edae..8f12665 100644 --- a/AbandonedCart/Block/System/Config/SyncAbandonedCartData.php +++ b/AbandonedCart/Block/System/Config/SyncAbandonedCartData.php @@ -144,7 +144,8 @@ public function getTotalAbandonedCart() ['eq' => CronConfig::NOT_SYNCED], ['eq' => CronConfig::FAIL_SYNCED] ] - ); + ) + ->addFieldToFilter('items_count',['gt' => 0]); return $collection->getSize(); } @@ -162,7 +163,8 @@ public function getNotSyncAbandonedCart() [ ['eq' => CronConfig::NOT_SYNCED] ] - ); + ) + ->addFieldToFilter('items_count',['gt' => 0]); return $collection->getSize(); } @@ -180,7 +182,8 @@ public function getFailedSync() [ ['eq' => CronConfig::FAIL_SYNCED] ] - ); + ) + ->addFieldToFilter('items_count',['gt' => 0]); return $collection->getSize(); } diff --git a/AbandonedCart/Model/AbandonedCartSendData.php b/AbandonedCart/Model/AbandonedCartSendData.php index 42483a3..427dfa1 100644 --- a/AbandonedCart/Model/AbandonedCartSendData.php +++ b/AbandonedCart/Model/AbandonedCartSendData.php @@ -176,7 +176,8 @@ public function __construct( DateTime $dateTime, CartRepositoryInterface $quoteRepository, UrlInterface $urlBuilder - ) { + ) + { $this->urlBuilder = $urlBuilder; $this->customerRepository = $customerRepository; $this->addressRepository = $addressRepository; @@ -210,7 +211,7 @@ public function __construct( public function sendAbandonedCartData($quoteId = null): array { $result = []; - $numberOfAbandonedCart = (int) $this->abandonedCartHelper->getNumberOfAbandonedCart(); + $numberOfAbandonedCart = (int)$this->abandonedCartHelper->getNumberOfAbandonedCart(); $abandonedCarts = $this->quoteResourceCollectionFactory->create() ->addFieldToSelect('*') ->addFieldToFilter('ac_synced_date', [ @@ -230,7 +231,7 @@ public function sendAbandonedCartData($quoteId = null): array $abandonedCarts->addFieldToFilter('entity_id', ['eq' => $quoteId]); } $abandonedCarts->setPageSize($numberOfAbandonedCart); - $abandonedCarts->getSelect()->join(array('address' => $abandonedCarts->getResource()->getTable('quote_address')),'main_table.entity_id = address.quote_id') + $abandonedCarts->getSelect()->join(array('address' => $abandonedCarts->getResource()->getTable('quote_address')), 'main_table.entity_id = address.quote_id') ->where("address.address_type='billing' and (main_table.customer_email is not null or address.email is not null)"); foreach ($abandonedCarts as $abandonedCart) { $connectionId = $this->coreHelper->getConnectionId($abandonedCart->getStoreId()); @@ -296,7 +297,7 @@ public function sendAbandonedCartData($quoteId = null): array $customerModel = $this->customerFactory->create(); $this->customerResource->load($customerModel, $customerId); if ($customerModel->getAcCustomerId()) { - $this->customerId = $customerModel->getAcCustomerId(); + $this->customerId = $customerModel->getAcCustomerId(); } } $abandonedCart->collectTotals(); @@ -311,7 +312,7 @@ public function sendAbandonedCartData($quoteId = null): array "orderDiscounts" => [ "discountAmount" => $this->coreHelper->priceToCents($abandonedCart->getDiscountAmount()) ], - "orderUrl" => $this->urlBuilder->getDirectUrl('checkout/cart'), + "orderUrl" => $this->urlBuilder->getDirectUrl('checkout/cart'), "abandonedDate" => $abandonedCart->getCreatedAt(), "externalCreatedDate" => $abandonedCart->getCreatedAt(), "externalUpdatedDate" => $abandonedCart->getUpdatedAt(), @@ -328,6 +329,7 @@ public function sendAbandonedCartData($quoteId = null): array ]; try { + if (is_null($abandonedCart->getAcSyncedDate())) { $abandonedCartResult = $this->curl->sendRequestAbandonedCart( self::METHOD, @@ -337,7 +339,7 @@ public function sendAbandonedCartData($quoteId = null): array } else { $abandonedCartResult = $this->curl->sendRequestAbandonedCart( self::UPDATE_METHOD, - self::ABANDONED_CART_URL_ENDPOINT . "/" . (int) $abandonedCart->getAcOrderSyncId(), + self::ABANDONED_CART_URL_ENDPOINT . "/" . (int)$abandonedCart->getAcOrderSyncId(), $abandonedCartData ); } @@ -407,7 +409,8 @@ private function getQuoteItems($quoteId): Collection $quoteItemCollection = $this->quoteItemCollectionFactory->create(); return $quoteItemCollection ->addFieldToSelect('*') - ->addFieldToFilter('quote_id', [$quoteId]); + ->addFieldToFilter('quote_id', [$quoteId]) + ->addFieldToFilter('parent_item_id', ['null' => true]); } /** @@ -461,7 +464,8 @@ private function getFieldValues($customerId): array return $fieldValues; } - public function isGuest($quote): bool{ + public function isGuest($quote): bool + { return is_null($quote->getCustomerId()); } @@ -510,6 +514,7 @@ private function createEcomCustomer($customerId, $quote): void $ecomCustomer['connectionid'] = $connectionid; $ecomCustomer['externalid'] = $customerId; $ecomCustomer['email'] = $customerEmail; + $ecomCustomer['acceptsMarketing'] = 1; $ecomCustomerData['ecomCustomer'] = $ecomCustomer; $AcCustomer = $this->curl->listAllCustomers( self::GET_METHOD, @@ -538,22 +543,35 @@ private function createEcomCustomer($customerId, $quote): void } if ($this->isGuest($quote)) { - $ecomCustomerData = [ - "ecomCustomer" => [ - "connection" => $connectionid, - 'externals' => $quote->getBillingAddress()->getEmail(), - 'email' => $quote->getBillingAddress()->getEmail(), - ] - ]; - $ecomCustomerResult = $this->curl->createContacts( - self::METHOD, + + $ecomCustomerResult = $this->curl->listAllCustomers( + self::GET_METHOD, self::ECOM_CUSTOMER_ENDPOINT, - $ecomCustomerData + $quote->getBillingAddress()->getEmail() ); - $ecomCustomerId = $ecomCustomerResult['data']['ecomCustomer']['id'] ?? null; + if (isset($ecomCustomerResult['data']['ecomCustomers'][0])) { + foreach ($ecomCustomerResult['data']['ecomCustomers'] as $Ac) { + if ($Ac['connectionid'] === $connectionid) { + $ecomCustomerId = $Ac['id']; + } + } + } else { + $ecomCustomerData = [ + "ecomCustomer" => [ + "connection" => $connectionid, + 'externals' => $quote->getBillingAddress()->getEmail(), + 'email' => $quote->getBillingAddress()->getEmail(), + ] + ]; + $ecomCustomerResult = $this->curl->createContacts( + self::METHOD, + self::ECOM_CUSTOMER_ENDPOINT, + $ecomCustomerData + ); + $ecomCustomerId = $ecomCustomerResult['data']['ecomCustomer']['id'] ?? null; + } } - - if ($ecomCustomerId !== 0) { + if ($ecomCustomerId !== 0) { $syncStatus = CronConfig::SYNCED; } else { $syncStatus = CronConfig::FAIL_SYNCED; @@ -564,7 +582,8 @@ private function createEcomCustomer($customerId, $quote): void } else { $this->saveCustomerResult($customerId, $syncStatus, $contactId, $ecomCustomerId); } - } catch (\Exception $e) { + } catch + (\Exception $e) { $this->logger->critical("MODULE AbandonedCart: " . $e->getMessage()); } catch (GuzzleException $e) { $this->logger->critical("MODULE AbandonedCart: " . $e->getMessage()); diff --git a/AbandonedCart/Ui/Component/Listing/AbandonedCartDataProvider.php b/AbandonedCart/Ui/Component/Listing/AbandonedCartDataProvider.php index ef28021..df02955 100644 --- a/AbandonedCart/Ui/Component/Listing/AbandonedCartDataProvider.php +++ b/AbandonedCart/Ui/Component/Listing/AbandonedCartDataProvider.php @@ -19,7 +19,7 @@ protected function _initSelect() 'main_table.entity_id = quote_address.quote_id', ['quote_address.quote_id','quote_address.email'], ) - ->where("main_table.is_active = 1 AND quote_address.address_type = 'billing'"); + ->where("main_table.is_active = 1 AND quote_address.address_type = 'billing' and main_table.items_count>0"); return $this; } } diff --git a/AbandonedCart/composer.json b/AbandonedCart/composer.json index c88cdc9..4d51dfa 100644 --- a/AbandonedCart/composer.json +++ b/AbandonedCart/composer.json @@ -9,7 +9,7 @@ "config": { "sort-packages": true }, - "version": "2.1.0", + "version": "2.1.1", "require": { "php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2", "activecampaign/core": "2.1.*" diff --git a/AbandonedCart/etc/module.xml b/AbandonedCart/etc/module.xml index 2359d77..c4a2876 100644 --- a/AbandonedCart/etc/module.xml +++ b/AbandonedCart/etc/module.xml @@ -6,7 +6,7 @@ */ --> - + diff --git a/Core/Controller/Adminhtml/System/Config/Connect.php b/Core/Controller/Adminhtml/System/Config/Connect.php index e19308c..f40c1cf 100644 --- a/Core/Controller/Adminhtml/System/Config/Connect.php +++ b/Core/Controller/Adminhtml/System/Config/Connect.php @@ -46,9 +46,11 @@ class Connect extends \Magento\Backend\App\Action */ private $curl; + private $cacheTypeList; + + /** - * Construct - * + * Connect constructor. * @param \Magento\Backend\App\Action\Context $context * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory * @param \Magento\Framework\App\Config\ConfigResource\ConfigInterface $configInterface @@ -56,6 +58,7 @@ class Connect extends \Magento\Backend\App\Action * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \ActiveCampaign\Core\Helper\Data $activeCampaignHelper * @param \ActiveCampaign\Core\Helper\Curl $curl + * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList */ public function __construct( \Magento\Backend\App\Action\Context $context, @@ -64,7 +67,8 @@ public function __construct( \Magento\Store\Api\StoreRepositoryInterface $storeRepository, \Magento\Store\Model\StoreManagerInterface $storeManager, \ActiveCampaign\Core\Helper\Data $activeCampaignHelper, - \ActiveCampaign\Core\Helper\Curl $curl + \ActiveCampaign\Core\Helper\Curl $curl, + \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList ) { parent::__construct($context); $this->resultJsonFactory = $resultJsonFactory; @@ -73,6 +77,8 @@ public function __construct( $this->storeManager = $storeManager; $this->activeCampaignHelper = $activeCampaignHelper; $this->curl = $curl; + $this->cacheTypeList = $cacheTypeList; + } /** @@ -99,7 +105,7 @@ public function execute() $this->saveConfig( \ActiveCampaign\Core\Helper\Data::ACTIVE_CAMPAIGN_GENERAL_STATUS, $request['status'], - $request['store'] + (int)$request['store'] ); } @@ -108,7 +114,7 @@ public function execute() $this->saveConfig( \ActiveCampaign\Core\Helper\Data::ACTIVE_CAMPAIGN_GENERAL_API_URL, $request['api_url'], - $request['store'] + (int)$request['store'] ); } @@ -117,7 +123,7 @@ public function execute() $this->saveConfig( \ActiveCampaign\Core\Helper\Data::ACTIVE_CAMPAIGN_GENERAL_API_KEY, $request['api_key'], - $request['store'] + (int)$request['store'] ); } @@ -177,6 +183,8 @@ public function execute() $checkConnections = $this->activeCampaignHelper->checkConnections($allConnections); $return['success'] = $checkConnections; + $this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER); + } catch (\Exception $e) { $return['success'] = false; $return['errorMessage'] = __($e->getMessage()); diff --git a/Core/Controller/Adminhtml/System/Config/Disconnect.php b/Core/Controller/Adminhtml/System/Config/Disconnect.php index f6e0e42..a4c582e 100644 --- a/Core/Controller/Adminhtml/System/Config/Disconnect.php +++ b/Core/Controller/Adminhtml/System/Config/Disconnect.php @@ -109,7 +109,7 @@ public function execute() $urlEndpoint = self::URL_ENDPOINT . '/' . $connectionId; $result = $this->curl->deleteConnection(self::METHOD, $urlEndpoint); - if ($result['success']) { + if ($result['success'] || str_contains($result['message'],'403 Forbidden')) { $this->configInterface->deleteConfig( \ActiveCampaign\Core\Helper\Data::ACTIVE_CAMPAIGN_GENERAL_CONNECTION_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORES, @@ -128,13 +128,13 @@ public function execute() $urlEndpoint = self::URL_ENDPOINT . '/' . $connectionId; $result = $this->curl->deleteConnection(self::METHOD, $urlEndpoint); - if ($result['success']) { + if ($result['success'] || str_contains($result['message'],'403 Forbidden')) { $this->configInterface->deleteConfig( \ActiveCampaign\Core\Helper\Data::ACTIVE_CAMPAIGN_GENERAL_CONNECTION_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORES, $store->getId() ); - } else { + }else{ $return['success'] = false; $return['errorMessage'] = $result['message']; } @@ -142,6 +142,7 @@ public function execute() } } + $allConnections = $this->curl->getAllConnections( self::GET_METHOD, self::URL_ENDPOINT @@ -154,12 +155,13 @@ public function execute() } } - if ($return['success'] === true) { + // if ($return['success'] === true) { $this->cacheTypeList->invalidate([ \Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER, \Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER ]); - } + $this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER); + //} $resultJson = $this->resultJsonFactory->create(); diff --git a/Core/composer.json b/Core/composer.json index 6ba64b2..53af9e7 100644 --- a/Core/composer.json +++ b/Core/composer.json @@ -9,7 +9,7 @@ "config": { "sort-packages": true }, - "version": "2.1.0", + "version": "2.1.1", "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 5686a9f..37f3908 100644 --- a/Core/etc/module.xml +++ b/Core/etc/module.xml @@ -1,4 +1,4 @@ - + diff --git a/Customer/Cron/CustomerSync.php b/Customer/Cron/CustomerSync.php index 3a0579e..fa54e14 100644 --- a/Customer/Cron/CustomerSync.php +++ b/Customer/Cron/CustomerSync.php @@ -152,6 +152,7 @@ public function execute() $ecomCustomer['connectionid'] = $this->coreHelper->getConnectionId($customer->getStoreId()); $ecomCustomer['externalid'] = $customerId; $ecomCustomer['email'] = $customer->getEmail(); + $ecomCustomer['acceptsMarketing'] = 1; $ecomCustomerData['ecomCustomer'] = $ecomCustomer; $ecomCustomerResult = $this->curl->createContacts( self::METHOD, @@ -260,6 +261,7 @@ private function contactBody($customer){ $contact['firstName'] = $customer->getFirstname(); $contact['lastName'] = $customer->getLastname(); $contact['phone'] = $this->getTelephone($customer->getDefaultBilling()); + $contact['fieldValues'] = $this->getFieldValues($customerId); $contactData['contact'] = $contact; diff --git a/Customer/Model/Customer.php b/Customer/Model/Customer.php index 3eb1e9b..9f2f77c 100644 --- a/Customer/Model/Customer.php +++ b/Customer/Model/Customer.php @@ -109,6 +109,7 @@ public function getEcomCustomerData($customerId) $ecomCustomer['connectionid'] = $this->coreHelper->getConnectionId($customer->getStoreId()); $ecomCustomer['externalid'] = $customer->getId(); $ecomCustomer['email'] = $customer->getEmail(); + $ecomCustomerData['acceptsMarketing'] = 1; $ecomCustomerData['ecomCustomer'] = $ecomCustomer; return $ecomCustomerData; diff --git a/Customer/composer.json b/Customer/composer.json index efb8a84..8525c1c 100644 --- a/Customer/composer.json +++ b/Customer/composer.json @@ -9,7 +9,7 @@ "config": { "sort-packages": true }, - "version": "2.1.2", + "version": "2.1.3", "require": { "php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2", "activecampaign/core": "2.1.*" diff --git a/Customer/etc/module.xml b/Customer/etc/module.xml index e4978fd..506b145 100644 --- a/Customer/etc/module.xml +++ b/Customer/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/Newsletter/Block/Adminhtml/System/Config/NewsletterSyncStatus.php b/Newsletter/Block/Adminhtml/System/Config/NewsletterSyncStatus.php new file mode 100644 index 0000000..b6f1047 --- /dev/null +++ b/Newsletter/Block/Adminhtml/System/Config/NewsletterSyncStatus.php @@ -0,0 +1,119 @@ +newsletterCollection = $newsletterCollection; + + parent::__construct($context, $data); + } + + /** + * Get order count helper + * @param array $filter + * @return int + */ + public function getNewsletterCountHelper(array $filter = []): int + { + + $this->newsletterCollection->clear()->getSelect()->reset(\Magento\Framework\Db\Select::WHERE); + if (count($filter)) { + foreach ($filter as $fil) { + $this->newsletterCollection->addFieldToFilter($fil['field'], $fil['value']); + } + } + + $this->newsletterCollection->setCurPage(1)->setPageSize(1); + + return $this->newsletterCollection->getSize(); + } + + /** + * Get total order + * @return int + */ + public function getTotalNewsletter(): int + { + return $this->getNewsletterCountHelper(); + } + + /** + * Get sync order + * + * @return int + */ + public function getSyncNewsletter(): int + { + + + return $this->getNewsletterCountHelper( + [ + ['field' => self::AC_SYNC_STATUS, + 'value' => ['eq' => \ActiveCampaign\Newsletter\Model\Config\CronConfig::SYNCED] + ] + ] + ); + } + + /** + * Get not sync order + * + * @return int + */ + public function getNotSyncNewsletter(): int + { + + return $this->getNewsletterCountHelper([ + [ + 'field' => self::AC_SYNC_STATUS, + 'value' => ['neq' => \ActiveCampaign\Order\Model\Config\CronConfig::SYNCED] + ] + ] + ); + } + + /** + * Get failed sync + * + * @return int + */ + public function getFailedSync(): int + { + + return $this->getNewsletterCountHelper([ + [ + 'field' => self::AC_SYNC_STATUS, + 'value' => ['eq' => \ActiveCampaign\Order\Model\Config\CronConfig::FAIL_SYNCED] + ] + ] + ); + + } +} diff --git a/Newsletter/Block/Adminhtml/System/Config/Widget.php b/Newsletter/Block/Adminhtml/System/Config/Widget.php new file mode 100644 index 0000000..5fb2495 --- /dev/null +++ b/Newsletter/Block/Adminhtml/System/Config/Widget.php @@ -0,0 +1,27 @@ +addChild('newsletter_sync_status', NewsletterSyncStatus::class); + + return parent::_prepareLayout(); + } + + /** + * @param \Magento\Framework\Data\Form\Element\AbstractElement $element + * @return string + */ + public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) + { + return $this->getChildHtml('newsletter_sync_status'); + } +} diff --git a/Newsletter/Cron/NewsletterSyncCron.php b/Newsletter/Cron/NewsletterSyncCron.php new file mode 100644 index 0000000..7e84490 --- /dev/null +++ b/Newsletter/Cron/NewsletterSyncCron.php @@ -0,0 +1,126 @@ +newsletterCollection = $newsletterCollection; + $this->activeCampaignHelper = $activeCampaignHelper; + $this->state = $state; + $this->curl = $curl; + $this->quoteRepository = $quoteRepository; + $this->logger = $logger; + } + + /** + * @throws NoSuchEntityException|GuzzleException + */ + public function execute(): void + { + try { + $isEnabled = $this->activeCampaignHelper->isNewslettersSyncEnabled(); + + if ($isEnabled) { + $newsletterSyncNum = $this->activeCampaignHelper->getNewsletterSyncNum(); + $newsletterCollection = $this->newsletterCollection + ->addFieldToSelect('*') + ->addFieldToFilter( + 'ac_newsletter_sync_status', + ['neq' => 1] + ) + ->addFieldToFilter( + 'customer_id', + ['eq' => 0] + ) + ->setPageSize($newsletterSyncNum) + ->setCurPage(1); + + foreach ($newsletterCollection as $news) { + + try { + $contactData = [ + 'contact' => [ + 'email' => $news->getSubscriberEmail() + ] + ]; + $contactResult = $this->curl->createContacts(self::METHOD, self::CONTACT_ENDPOINT, $contactData); + if (isset($contactResult['data']['contact']['id'])) { + $news->setAcNewsletterSyncId($contactResult['data']['contact']['id']); + $news->setAcNewsletterSyncStatus(1); + $news->save(); + } + } catch (NoSuchEntityException|GuzzleException $e) { + $this->logger->error('MODULE Order: ' . $e->getMessage()); + } + } + } + } catch (\Exception $e) { + $this->logger->error('MODULE Order: ' . $e->getMessage()); + } + + } +} diff --git a/Newsletter/Helper/Data.php b/Newsletter/Helper/Data.php new file mode 100644 index 0000000..a36a937 --- /dev/null +++ b/Newsletter/Helper/Data.php @@ -0,0 +1,58 @@ +state = $state; + } + + /** + * @param null $scopeCode + * @return bool + */ + public function isNewslettersSyncEnabled($scopeCode = null) + { + return $this->scopeConfig->isSetFlag( + self::ACTIVE_CAMPAIGN_NEWSLETTER_STATUS, + ScopeInterface::SCOPE_STORES, + $scopeCode + ); + } + + + /** + * @param null $scopeCode + * @return mixed + */ + public function getNewsletterSyncNum($scopeCode = null) + { + return $this->scopeConfig->getValue( + self::ACTIVE_CAMPAIGN_NEWSLETTER_SYNC_NUM, + ScopeInterface::SCOPE_STORES, + $scopeCode + ); + } +} diff --git a/Newsletter/Model/Config/CronConfig.php b/Newsletter/Model/Config/CronConfig.php new file mode 100644 index 0000000..4d01e95 --- /dev/null +++ b/Newsletter/Model/Config/CronConfig.php @@ -0,0 +1,113 @@ +_runModelPath = $runModelPath; + $this->_configValueFactory = $configValueFactory; + parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); + } + + /** + * {@inheritdoc} + * + * @return $this + * @throws \Exception + */ + public function afterSave() + { + $cronMinute = $this->getData('groups/newsletter_sync/fields/cron_minute/value'); + $cronHour = $this->getData('groups/newsletter_sync/fields/cron_hour/value'); + $cronDay = $this->getData('groups/newsletter_sync/fields/cron_day/value'); + $cronMonth = $this->getData('groups/v/fields/cron_month/value'); + $cronWeekday = $this->getData('groups/newsletter_sync/fields/cron_weekday/value'); + + $cronMinute = ($cronMinute == null) ? '*' : $cronMinute; + $cronHour = ($cronHour == null) ? '*' : $cronHour; + $cronDay = ($cronDay == null) ? '*' : $cronDay; + $cronMonth = ($cronMonth == null) ? '*' : $cronMonth; + $cronWeekday = ($cronWeekday == null) ? '*' : $cronWeekday; + + $cronExprArray = [ + $cronMinute, //Minute + $cronHour, //Hour + $cronDay, //Day of the Month + $cronMonth, //Month of the Year + $cronWeekday, //Day of the Week + ]; + + $cronExprString = join(' ', $cronExprArray); + + try { + $this->_configValueFactory->create()->load( + self::CRON_STRING_PATH, + 'path' + )->setValue( + $cronExprString + )->setPath( + self::CRON_STRING_PATH + )->save(); + $this->_configValueFactory->create()->load( + self::CRON_MODEL_PATH, + 'path' + )->setValue( + $this->_runModelPath + )->setPath( + self::CRON_MODEL_PATH + )->save(); + } catch (\Exception $e) { + throw new \Exception(__('We can\'t save the cron expression.')); + } + + return parent::afterSave(); + } +} diff --git a/Newsletter/composer.json b/Newsletter/composer.json new file mode 100644 index 0000000..c45b126 --- /dev/null +++ b/Newsletter/composer.json @@ -0,0 +1,25 @@ +{ + "name": "activecampaign/newsletter", + "description": "ActiveCampaign Newsletter", + "type": "magento2-module", + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "config": { + "sort-packages": true + }, + "version": "1.0.0", + "require": { + "php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2", + "activecampaign/core": "2.1.*" + }, + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "ActiveCampaign\\Order\\": "" + } + } +} diff --git a/Newsletter/etc/adminhtml/system.xml b/Newsletter/etc/adminhtml/system.xml new file mode 100644 index 0000000..a0581c1 --- /dev/null +++ b/Newsletter/etc/adminhtml/system.xml @@ -0,0 +1,65 @@ + + + +
+ + + + + Magento\Config\Model\Config\Source\Yesno + + + + + + 1 + + ActiveCampaign\Newsletter\Model\Config\CronConfig + + + + + + 1 + + + + + + + 1 + + + + + + + 1 + + + + + + + 1 + + + + + Number of Newsletters sync at a time + + + + ActiveCampaign\Newsletter\Block\Adminhtml\System\Config\Widget + + 1 + + + +
+
+
diff --git a/Newsletter/etc/config.xml b/Newsletter/etc/config.xml new file mode 100644 index 0000000..405535d --- /dev/null +++ b/Newsletter/etc/config.xml @@ -0,0 +1,17 @@ + + + + + + 0 + 100 + 5 + * + * + * + * + + + + diff --git a/Newsletter/etc/crontab.xml b/Newsletter/etc/crontab.xml new file mode 100644 index 0000000..709c8c6 --- /dev/null +++ b/Newsletter/etc/crontab.xml @@ -0,0 +1,10 @@ + + + + + crontab/default/jobs/ac_newsletter_sync_cron_job/schedule/cron_expr + + + diff --git a/Newsletter/etc/db_schema.xml b/Newsletter/etc/db_schema.xml new file mode 100644 index 0000000..defd7f6 --- /dev/null +++ b/Newsletter/etc/db_schema.xml @@ -0,0 +1,11 @@ + + + + + +
+ +
diff --git a/Newsletter/etc/module.xml b/Newsletter/etc/module.xml new file mode 100644 index 0000000..eba6651 --- /dev/null +++ b/Newsletter/etc/module.xml @@ -0,0 +1,9 @@ + + + + + + + + diff --git a/Newsletter/registration.php b/Newsletter/registration.php new file mode 100644 index 0000000..07ddc3c --- /dev/null +++ b/Newsletter/registration.php @@ -0,0 +1,9 @@ + + + + + + + + + + +
+
+
+
+ escapeHtml(__("Total Newsletters :")) ?> + escapeHtml(__($block->getTotalNewsletter())) ?> +
+
+
+
+
+
+ escapeHtml(__("Synced Newsletter :")) ?> + escapeHtml(__($block->getSyncNewsletter())) ?> +
+
+
+
+
+
+ escapeHtml(__("Not Synced Newsletter :")) ?> + escapeHtml(__($block->getNotSyncNewsletter())) ?> +
+
+
+
+
+
+ escapeHtml(__("Fail Synced Newsletter :")) ?> + escapeHtml(__($block->getFailedSync())) ?> +
+
+
+
+ diff --git a/composer.json b/composer.json index a0a1e4c..207f82c 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "activecampaign/module-integration", "description": "ActiveCampaign extension for Magento 2.3 and 2.4", "type": "magento2-component", - "version": "2.0.9", + "version": "2.0.10", "license": "OSL-3.0", "require": { "php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2" @@ -12,7 +12,8 @@ "activecampaign/customer": "self.version", "activecampaign/abandoned-cart": "self.version", "activecampaign/order": "self.version", - "activecampaign/sync-log": "self.version" + "activecampaign/sync-log": "self.version", + "activecampaign/newsletter": "self.version" }, "autoload": { "files": [ @@ -20,14 +21,16 @@ "Customer/registration.php", "AbandonedCart/registration.php", "Order/registration.php", - "SyncLog/registration.php" + "SyncLog/registration.php", + "Newsletter/registration.php" ], "psr-4": { "ActiveCampaign\\Core\\": "Core", "ActiveCampaign\\Customer\\": "Customer", "ActiveCampaign\\AbandonedCart\\": "AbandonedCart", "ActiveCampaign\\Order\\": "Order", - "ActiveCampaign\\SyncLog\\": "SyncLog" + "ActiveCampaign\\SyncLog\\": "SyncLog", + "ActiveCampaign\\Newsletter\\": "Newsletter" } }, "minimum-stability": "dev", diff --git a/marketplace-composer.json b/marketplace-composer.json index 317ba7a..59a868c 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.0.9", + "version": "2.0.10", "license": [ "OSL-3.0" ], @@ -11,6 +11,7 @@ "activecampaign/customer": "2.1.*", "activecampaign/abandonedcart": "2.1.*", "activecampaign/order": "2.1.*", - "activecampaign/synclog": "2.1.*" + "activecampaign/synclog": "2.1.*", + "activecampaign/newsletter": "1.0.*" } } From 4000bf9ecdaa7a2f4779e2e7adc61aa581b76cac Mon Sep 17 00:00:00 2001 From: Jorge Venzon Date: Mon, 28 Aug 2023 17:39:23 -0300 Subject: [PATCH 2/2] composer update --- Newsletter/composer.json | 4 ++-- Newsletter/etc/module.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Newsletter/composer.json b/Newsletter/composer.json index c45b126..6a74f00 100644 --- a/Newsletter/composer.json +++ b/Newsletter/composer.json @@ -9,7 +9,7 @@ "config": { "sort-packages": true }, - "version": "1.0.0", + "version": "1.0.1", "require": { "php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2", "activecampaign/core": "2.1.*" @@ -19,7 +19,7 @@ "registration.php" ], "psr-4": { - "ActiveCampaign\\Order\\": "" + "ActiveCampaign\\Newsletter\\": "" } } } diff --git a/Newsletter/etc/module.xml b/Newsletter/etc/module.xml index eba6651..ab2e72d 100644 --- a/Newsletter/etc/module.xml +++ b/Newsletter/etc/module.xml @@ -1,7 +1,7 @@ - +