弃车观察员


8

我需要帮助!!!

我正在创建一个通过API与另一个平台集成的模块。我已经获得了用于客户注册的观察员或事件(customer_register_success),购买已完成(checkout_onepage_controller_success_action)。

我需要的帮助是如何将观察者带到废弃的购物车上?或者哪种方法是获取该信息并通过API发送信息的最佳方法。


2
您对餐车的定义是什么?
菲利普·桑德

当客户将产品添加到汽车上却没有购买时
Knaimero

3
没在2分钟,10分钟,1小时或一天之内进行购买吗?无论如何,无论您为定义选择什么时间,都可以观察到发生的事件,而不是未发生的事件。我认为,对于您的用例而言,最好的概念是cronjob,它会检查最后一次交互作用早于x分钟/小时/天的活动报价。
HelgeB

谢谢。Magento在您进行购买并关闭电子商务时,会自动创建一条记录,您可以在管理->报告->废弃的购物车中查看。我的问题存在,有没有办法获取这些信息?
Knaimero

没有此类事件可以获取,您可以获得在特定时间之间未排序的报价收集
Ketan Borada

Answers:


3
  • 没有这样的事件来得到废弃的购物车,您必须自定义它。
  • 我有克服的想法,您必须创建在每个特定时间运行的cron并收集所有未排序的报价以及您设置的时间之间(创建的报价时间与更新的报价时间之间的差)。你只需要管理updatedAtFromupdatedAtTo
  • 这样,您将收集所有报价数据,并且在该集合中,您可以调度事件并将所有报价数据和客户数据传递给单个事件内的事件,也可以对所有报价进行合理化处理,然后将数据从观察者传递给API。

我创建了可以在您的阻止功能中应用的脚本,我正在使用此工作脚本将购物车中的商品通过邮件发送给我的客户,因为他们没有订单就离开了。

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

?>

以上脚本的结果查询为:

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 
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.