Skip to content

Commit f6a676b

Browse files
authored
Merge pull request #11 from landofcoder/feature-abandonded-cart
Feature abandonded cart
2 parents 8a80e80 + b99297d commit f6a676b

35 files changed

+1303
-178
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
./dev
33
.test
44
.test/*
5+
.ideal
6+
.ideal/
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace Lof\Mautic\Block\Adminhtml\System\Config;
3+
4+
class Date extends \Magento\Config\Block\System\Config\Form\Field
5+
{
6+
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
7+
{
8+
$element->setDateFormat(\Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT);
9+
$element->setTimeFormat(null);
10+
return parent::render($element);
11+
}
12+
13+
}

Block/Loadquote.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://landofcoder.com/terms
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_Mautic
18+
* @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
19+
* @license https://landofcoder.com/terms
20+
*/
21+
22+
namespace Lof\Mautic\Block;
23+
24+
use Magento\Framework\View\Element\Template;
25+
26+
class Loadquote extends Template
27+
{
28+
/**
29+
* @var string $_template
30+
*/
31+
protected $_template = "Lof_Mautic::loadquote.phtml";
32+
33+
// write your methods here...
34+
}
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace Lof\Mautic\Console\Command;
4+
5+
use Lof\Mautic\Queue\Processor\AbandonedCartProcessorFactory;
6+
use Magento\Framework\App\Area;
7+
use Magento\Framework\App\State;
8+
use Magento\Framework\Registry;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
/**
14+
* Class AbandonedCartCommand
15+
*/
16+
class AbandonedCartCommand extends Command
17+
{
18+
/**
19+
* @var AbandonedCartProcessorFactory
20+
*/
21+
private $abandonedCartProcessorFactory;
22+
/**
23+
* @var State
24+
*/
25+
private $state;
26+
27+
/**
28+
* @var Registry
29+
*/
30+
private $registry;
31+
32+
/**
33+
* CategoryImport constructor.
34+
*
35+
* @param AbandonedCartProcessorFactory $abandonedCartProcessorFactory
36+
* @param State $state
37+
* @param Registry $registry
38+
* @param null $name
39+
*/
40+
public function __construct(
41+
AbandonedCartProcessorFactory $abandonedCartProcessorFactory,
42+
State $state,
43+
Registry $registry,
44+
$name = null
45+
) {
46+
parent::__construct($name);
47+
48+
$this->abandonedCartProcessorFactory = $abandonedCartProcessorFactory;
49+
$this->state = $state;
50+
$this->registry = $registry;
51+
}
52+
53+
/**
54+
* Configures the current command.a
55+
*
56+
* @return void
57+
*/
58+
public function configure()
59+
{
60+
$this->setName('lofmautic:export:abandoned');
61+
$this->setDescription('Process export all contacts Data from Abandoned Cart to Mautic');
62+
}
63+
64+
/**
65+
* @param InputInterface $input
66+
* @param OutputInterface $output
67+
*
68+
* @return int|null
69+
*
70+
*/
71+
protected function execute(InputInterface $input, OutputInterface $output)
72+
{
73+
try {
74+
$this->state->setAreaCode(Area::AREA_ADMINHTML);
75+
} catch (\Exception $ex) {
76+
// fail gracefully
77+
}
78+
79+
if (!$this->registry->registry('isSecureArea')) {
80+
$this->registry->register('isSecureArea', true);
81+
}
82+
83+
$start = $this->getCurrentMs();
84+
85+
$output->writeln('<info>Initialization exporting of contacts of all customers.</info>');
86+
$output->writeln(sprintf('<info>Started at %s</info>', (new \DateTime())->format('Y-m-d H:i:s')));
87+
$output->writeln('Exporting...');
88+
89+
$abandonedCartProcessor = $this->abandonedCartProcessorFactory->create();
90+
91+
$abandonedCartProcessor->process();
92+
93+
$end = $this->getCurrentMs();
94+
95+
$output->writeln(sprintf('<info>Finished at %s</info>', (new \DateTime())->format('Y-m-d H:i:s')));
96+
$output->writeln(sprintf('<info>Total execution time %sms</info>', $end - $start));
97+
98+
return 0;
99+
}
100+
101+
/**
102+
*
103+
* @return float|int
104+
*/
105+
private function getCurrentMs()
106+
{
107+
$mt = explode(' ', microtime());
108+
109+
return ((int) $mt[1]) * 1000 + ((int) round($mt[0] * 1000));
110+
}
111+
}

Controller/Cart/Loadquote.php

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://landofcoder.com/terms
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_Mautic
18+
* @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
19+
* @license https://landofcoder.com/terms
20+
*/
21+
namespace Lof\Mautic\Controller\Cart;
22+
23+
use Magento\Framework\App\Action\Action;
24+
use Magento\Framework\Controller\ResultFactory;
25+
use Magento\Framework\View\Result\PageFactory;
26+
use Magento\Framework\App\Action\Context;
27+
use Lof\Mautic\Queue\MessageQueues\Order\Publisher;
28+
use Lof\Mautic\Model\Mautic\Contact;
29+
use Lof\Mautic\Helper\Data;
30+
31+
class Loadquote extends \Magento\Framework\App\Action\Action
32+
{
33+
/**
34+
* @var Publisher|null
35+
*/
36+
private $_publisher = null;
37+
38+
/**
39+
* @var Lof\Mautic\Model\Mautic\Contact|null
40+
*/
41+
protected $_mauticContact = null;
42+
43+
/**
44+
* @var PageFactory
45+
*/
46+
protected $pageFactory;
47+
/**
48+
* @var \Magento\Quote\Model\QuoteFactory
49+
*/
50+
protected $_quote;
51+
/**
52+
* @var \Magento\Customer\Model\Session
53+
*/
54+
protected $_customerSession;
55+
/**
56+
* @var \Ebizmarts\MailChimp\Helper\Data
57+
*/
58+
protected $_helper;
59+
/**
60+
* @var \Magento\Framework\Url
61+
*/
62+
protected $_urlHelper;
63+
/**
64+
* @var \Magento\Framework\Message\ManagerInterface
65+
*/
66+
protected $_message;
67+
/**
68+
* @var \Magento\Customer\Model\Url
69+
*/
70+
protected $_customerUrl;
71+
/**
72+
* @var \Magento\Checkout\Model\Session
73+
*/
74+
protected $_checkoutSession;
75+
76+
/**
77+
* Loadquote constructor.
78+
* @param Context $context
79+
* @param PageFactory $pageFactory
80+
* @param \Magento\Quote\Model\QuoteFactory $quote
81+
* @param \Magento\Customer\Model\Session $customerSession
82+
* @param \Magento\Checkout\Model\Session $checkoutSession
83+
* @param \Lof\Mautic\Helper\Data $helper
84+
* @param \Magento\Framework\Url $urlHelper
85+
* @param \Magento\Customer\Model\Url $customerUrl
86+
* @param Publisher $publisher
87+
* @param Contact $mauticContact
88+
*/
89+
public function __construct(
90+
Context $context,
91+
PageFactory $pageFactory,
92+
\Magento\Quote\Model\QuoteFactory $quote,
93+
\Magento\Customer\Model\Session $customerSession,
94+
\Magento\Checkout\Model\Session $checkoutSession,
95+
\Lof\Mautic\Helper\Data $helper,
96+
\Magento\Framework\Url $urlHelper,
97+
\Magento\Customer\Model\Url $customerUrl,
98+
Publisher $publisher,
99+
Contact $mauticContact
100+
) {
101+
102+
$this->pageFactory = $pageFactory;
103+
$this->_quote = $quote;
104+
$this->_customerSession = $customerSession;
105+
$this->_helper = $helper;
106+
$this->_urlHelper = $urlHelper;
107+
$this->_message = $context->getMessageManager();
108+
$this->_customerUrl = $customerUrl;
109+
$this->_checkoutSession = $checkoutSession;
110+
$this->_publisher = $publisher;
111+
$this->_mauticContact = $mauticContact;
112+
113+
parent::__construct($context);
114+
}
115+
116+
public function execute()
117+
{
118+
/** @var \Magento\Framework\View\Result\Page $resultPage */
119+
$resultPage = $this->pageFactory->create();
120+
$params = $this->getRequest()->getParams();
121+
if (isset($params['id']) && $params['id']) {
122+
$quote = $this->_quote->create();
123+
$quote->getResource()->load($quote, $params['id']);
124+
$magentoStoreId = $quote->getStoreId();
125+
$configSecretKey = $this->_helper->getConfig(\Lof\Mautic\Helper\Data::MODULE_ABANDONEDCART_TOKEN);
126+
127+
if (!isset($params['token']) || $params['token'] != $configSecretKey) {
128+
// @error
129+
$this->_message->addErrorMessage(__("You can't access this cart"));
130+
$url = $this->_urlHelper->getUrl(
131+
$this->_helper->getConfig(
132+
\Lof\Mautic\Helper\Data::MODULE_ABANDONEDCART_PAGE,
133+
$magentoStoreId
134+
)
135+
);
136+
$this->_redirect($url);
137+
} else {
138+
if (isset($params['mt_cid'])) {
139+
$url = $this->_urlHelper->getUrl(
140+
$this->_helper->getConfig(
141+
\Lof\Mautic\Helper\Data::MODULE_ABANDONEDCART_PAGE,
142+
$magentoStoreId
143+
),
144+
['mt_cid'=> $params['mt_cid']]
145+
);
146+
$quote->setData('mautic_campaign_id', $params['mt_cid']);
147+
} else {
148+
$url = $this->_urlHelper->getUrl(
149+
$this->_helper->getConfig(
150+
\Lof\Mautic\Helper\Data::MODULE_ABANDONEDCART_PAGE,
151+
$magentoStoreId
152+
)
153+
);
154+
}
155+
156+
$quote->getResource()->save($quote);
157+
158+
if ($emailAddress = $quote->getCustomerEmail()) {
159+
$this->processUpdateContactTag($emailAddress, $quote->getStoreId());
160+
}
161+
162+
if (!$quote->getCustomerId()) {
163+
$this->_checkoutSession->setQuoteId($quote->getId());
164+
$this->_redirect($url);
165+
} else {
166+
if ($this->_customerSession->isLoggedIn()) {
167+
$this->_redirect($url);
168+
} else {
169+
$this->_message->addNoticeMessage(__("Login to complete your order"));
170+
if (isset($params['mt_cid'])) {
171+
$url = $this->_urlHelper->getUrl(
172+
$this->_customerUrl->getLoginUrl(),
173+
['mt_cid'=>$params['mt_cid']]
174+
);
175+
} else {
176+
$url = $this->_customerUrl->getLoginUrl();
177+
}
178+
$this->_redirect($url);
179+
}
180+
}
181+
}
182+
}
183+
return $resultPage;
184+
}
185+
186+
/**
187+
* process update contact tags
188+
*
189+
* @param string $email
190+
* @param int|string|null $storeId
191+
* @return bool
192+
*/
193+
protected function processUpdateContactTag($email, $storeId = null)
194+
{
195+
if ($this->_helper->isEnabled($storeId)) {
196+
$removeTags = "-".(Data::ABANDONED_CART_TAGS);
197+
$data = [
198+
"email" => $email,
199+
"tags" => $removeTags
200+
];
201+
if (!$this->_helper->isAyncApi($storeId)) {
202+
$this->_mauticContact->exportContact($data);
203+
} else {
204+
$this->_publisher->execute(
205+
$this->_helper->encodeData($data)
206+
);
207+
}
208+
}
209+
return true;
210+
}
211+
}

0 commit comments

Comments
 (0)