Skip to content

Commit

Permalink
Merge pull request #62 from jorgeeurekalabs/main
Browse files Browse the repository at this point in the history
Newslleter
  • Loading branch information
jorgeeurekalabs authored Aug 28, 2023
2 parents bf8b512 + 4000bf9 commit fbf43ff
Show file tree
Hide file tree
Showing 34 changed files with 729 additions and 96 deletions.
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

5 changes: 0 additions & 5 deletions .idea/inspectionProfiles/profiles_settings.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/magento2plugin.xml

This file was deleted.

16 changes: 0 additions & 16 deletions .idea/module-integration.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/php.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

9 changes: 6 additions & 3 deletions AbandonedCart/Block/System/Config/SyncAbandonedCartData.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ public function getTotalAbandonedCart()
['eq' => CronConfig::NOT_SYNCED],
['eq' => CronConfig::FAIL_SYNCED]
]
);
)
->addFieldToFilter('items_count',['gt' => 0]);

return $collection->getSize();
}
Expand All @@ -162,7 +163,8 @@ public function getNotSyncAbandonedCart()
[
['eq' => CronConfig::NOT_SYNCED]
]
);
)
->addFieldToFilter('items_count',['gt' => 0]);

return $collection->getSize();
}
Expand All @@ -180,7 +182,8 @@ public function getFailedSync()
[
['eq' => CronConfig::FAIL_SYNCED]
]
);
)
->addFieldToFilter('items_count',['gt' => 0]);

return $collection->getSize();
}
Expand Down
63 changes: 41 additions & 22 deletions AbandonedCart/Model/AbandonedCartSendData.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ public function __construct(
DateTime $dateTime,
CartRepositoryInterface $quoteRepository,
UrlInterface $urlBuilder
) {
)
{
$this->urlBuilder = $urlBuilder;
$this->customerRepository = $customerRepository;
$this->addressRepository = $addressRepository;
Expand Down Expand Up @@ -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', [
Expand All @@ -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());
Expand Down Expand Up @@ -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();
Expand All @@ -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(),
Expand All @@ -328,6 +329,7 @@ public function sendAbandonedCartData($quoteId = null): array
];

try {

if (is_null($abandonedCart->getAcSyncedDate())) {
$abandonedCartResult = $this->curl->sendRequestAbandonedCart(
self::METHOD,
Expand All @@ -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
);
}
Expand Down Expand Up @@ -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]);
}

/**
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion AbandonedCart/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.0",
"version": "2.1.1",
"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 AbandonedCart/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="ActiveCampaign_AbandonedCart" setup_version="2.1.0">
<module name="ActiveCampaign_AbandonedCart" setup_version="2.1.1">
<sequence>
<module name="Magento_Product"/>
<module name="Magento_Sales"/>
Expand Down
20 changes: 14 additions & 6 deletions Core/Controller/Adminhtml/System/Config/Connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ 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
* @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
* @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,
Expand All @@ -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;
Expand All @@ -73,6 +77,8 @@ public function __construct(
$this->storeManager = $storeManager;
$this->activeCampaignHelper = $activeCampaignHelper;
$this->curl = $curl;
$this->cacheTypeList = $cacheTypeList;

}

/**
Expand All @@ -99,7 +105,7 @@ public function execute()
$this->saveConfig(
\ActiveCampaign\Core\Helper\Data::ACTIVE_CAMPAIGN_GENERAL_STATUS,
$request['status'],
$request['store']
(int)$request['store']
);
}

Expand All @@ -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']
);
}

Expand All @@ -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']
);
}

Expand Down Expand Up @@ -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());
Expand Down
12 changes: 7 additions & 5 deletions Core/Controller/Adminhtml/System/Config/Disconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -128,20 +128,21 @@ 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'];
}
}
}
}


$allConnections = $this->curl->getAllConnections(
self::GET_METHOD,
self::URL_ENDPOINT
Expand All @@ -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();

Expand Down
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.0",
"version": "2.1.1",
"require": {
"php": "~7.3.0||~7.4.0||~8.0||~8.1||~8.2"
},
Expand Down
Loading

0 comments on commit fbf43ff

Please sign in to comment.