8

I need help!!!

I am creating a module that integrates with another platform via API. I already got the Observer or Event for customer registration (customer_register_success), purchase completed (checkout_onepage_controller_success_action).

The help I need is how I can get the Observer to abandoned carts?, or what would be the best method to obtain that information and send it via API.

  • 2
    what is your definition of abadoned carts? – Philipp Sander Jun 14 at 14:44
  • When the customer added products to the car and did not make the purchase – Knaimero Jun 14 at 17:05
  • 3
    Did not make the purchase within 2 minutes, 10 minutes, one hour or a day? Anyway, whatever time you choose for your definition, you can observe an event which happens, and not something which not happens. In my opinion for your usecase the best concept would be a cronjob which checks active quotes with last interaction older than x minutes/hours/days. – HelgeB Jun 15 at 6:33
  • Thank you. Magento when you are making a purchase and close the ecommerce, automatically creates a record that you can see in the admin -> reports -> abandoned Carts. My question exists is there a way to get that information? – Knaimero Jun 17 at 16:10
  • There is no such event to get it,you can get quote collection which is not ordered between specific time – Ketan Borada Jun 19 at 6:51
3
+50
  • There is no such event to get abandoned carts,you have to create it custom.
  • I have idea to overcome this,you have to create cron that run every specific time and collect all quote which is not ordered and between time you set(difference between created and updated quote time). you have to just manage updatedAtFrom and updatedAtTo
  • By doing this you will collect all quotes data and in that collection you can dispatch event and pass all quote data and customer data to that event within single event or saperate for all quote and pass that data to API from observer.

I have created Script that you can apply in your Block function i'm using this working script for send cart item in mail to my customer after they left without order.

<?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
        }

?>

Resulted Query of above script is :

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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.