- Non esiste un evento del genere per ottenere carrelli abbandonati, è necessario crearlo personalizzato.
- Ho idea di ovviare a questo, devi creare cron che viene eseguito ogni volta specifico e raccogliere tutte le quote che non sono ordinate e tra il tempo impostato (differenza tra il tempo di citazione creato e aggiornato). devi solo gestire
updatedAtFrom
eupdatedAtTo
- In questo modo raccoglierai tutti i dati delle citazioni e in quella raccolta potrai inviare eventi e trasmettere tutti i dati delle citazioni e i dati dei clienti a quell'evento all'interno di un singolo evento o scegliere per tutte le quotazioni e passare tali dati all'API dall'osservatore.
Ho creato Script che puoi applicare nella tua funzione di blocco. Sto usando questo script funzionante per inviare l'articolo del carrello per posta ai miei clienti dopo che sono partiti senza ordine.
<?php
ob_start();
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
ini_set('memory_limit', '1024M');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->create('Magento\Framework\App\ResourceConnection');
$updatedAtFrom = '2019-06-19 19:00:00'; //Add current time
$updatedAtTo = '2019-06-19 20:30:00'; // $updatedAtFrom + 90 minutes ,add 90 minutes in current time to abondened cart
$connection = $resource->getConnection();
$select = $connection->select()
->from(
['q' => $resource->getTableName('quote')],
[
'store_id' => 'q.store_id',
'quote_id' => 'q.entity_id',
'customer_id' => 'q.customer_id',
'updated_at' => 'q.updated_at',
'created_at' => 'q.created_at',
]
)
->joinLeft(
['qa' => $resource->getTableName('quote_address')],
'q.entity_id = qa.quote_id AND qa.address_type = "billing"',
[
'customer_email' => new \Zend_Db_Expr('IFNULL(q.customer_email, qa.email)'),
'customer_firstname' => new \Zend_Db_Expr('IFNULL(q.customer_firstname, qa.firstname)'),
'customer_lastname' => new \Zend_Db_Expr('IFNULL(q.customer_lastname, qa.lastname)'),
]
)
->joinInner(
['qi' => $resource->getTableName('quote_item')],
'q.entity_id = qi.quote_id',
[
'i_created_at' => new \Zend_Db_Expr('MAX(qi.created_at)'),
]
)
->joinLeft(array('order' => $resource->getTableName('sales_order')),
'order.quote_id = q.entity_id',
array()
)
->where('order.entity_id IS NULL')
->where('q.is_active = 1')
->where('q.items_count > 0')
->where('q.customer_email IS NOT NULL OR qa.email IS NOT NULL')
->where('qi.parent_item_id IS NULL')
->group('q.entity_id')
->having(
'(q.created_at > ? OR MAX(qi.created_at) > ?)',
$updatedAtFrom
)
->having(
'(q.created_at < ? OR MAX(qi.created_at) < ?)',
$updatedAtTo
)
->order('q.updated_at');
$quotes = $connection->fetchAll($select);
foreach ($quotes as $quote) {
$params = [
'store_id' => $quote['store_id'],
'quote_id' => $quote['quote_id'],
'customer_id' => $quote['customer_id'],
'customer_email' => $quote['customer_email'],
'customer_tname' => $quote['customer_firstname'] . ' ' . $quote['customer_lastname'],
'created_at' => max($quote['created_at'], $quote['i_created_at']),
];
echo $quote['quote_id'];
/*$this->eventdispatch->register(
'quote_abandoned',
[$params['quote_id']],
$params
);*/
// Dispatch Event here and writelogic in that event which you want
}
?>
La query risultante dello script precedente è:
SELECT `q`.`store_id`, `q`.`entity_id` AS `quote_id`, `q`.`customer_id`, `q`.`updated_at`, `q`.`created_at`, IFNULL(q.customer_email, qa.email) AS `customer_email`, IFNULL(q.customer_firstname, qa.firstname) AS `customer_firstname`, IFNULL(q.customer_lastname, qa.lastname) AS `customer_lastname`, MAX(qi.created_at) AS `i_created_at` FROM `quote` AS `q` LEFT JOIN `quote_address` AS `qa` ON q.entity_id = qa.quote_id AND qa.address_type = "billing" INNER JOIN `quote_item` AS `qi` ON q.entity_id = qi.quote_id LEFT JOIN `sales_order` AS `order` ON order.quote_id = q.entity_id WHERE (order.entity_id IS NULL) AND (q.is_active = 1) AND (q.items_count > 0) AND (q.customer_email IS NOT NULL OR qa.email IS NOT NULL) AND (qi.parent_item_id IS NULL) GROUP BY `q`.`entity_id` HAVING ((q.created_at > '2019-06-19 19:00:00' OR MAX(qi.created_at) > '2019-06-19 19:00:00')) AND ((q.created_at < '2019-06-19 20:30:00' OR MAX(qi.created_at) < '2019-06-19 20:30:00')) ORDER BY `q`.`updated_at` ASC