1

I want to find inactive customer list that is customers who are inactive from a certain amount of time I have an account creation date of the customer if I get last visited date then I can put logic to get inactive customers.

1

Magento saves customer last visited date at customer_visitor table at the database. This table is related to the collection class Magento\Customer\Model\ResourceModel\Visitor\Collection.

At, customer_visitor table's last_visit_at column save customer last visited time.

So, you want to get a collection of the customer which is not visited at the system, then used below:

<?php

namespace StackExchange\Magento\Test;


class InActiveCustomer {

    /**
     * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
     */
    private $_localeDate;

    /**
     * @var \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory
     */
    private $visitorCollectionFactory;

    public function __construct(
        \Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory $visitorCollectinFactory,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $_localeDate    
    ) {

        $this->visitorCollectionFactory = $visitorCollectinFactory;
        $this->_localeDate = $_localeDate;
    }
    public function getInActiveCustomer()
    {

        $from = strtotime('-7 day', strtotime(date("Y-m-d h:i:s")));

        $visitorCollection = $this->visitorCollectionFactory->create();
        $visitorCollection->addFieldToSelect('*'); // Select all field
        $visitorCollection->addFieldToFilter('customer', ['gteq' => 1 ]);
        // Filter the Collection by 7 Old
        $visitorCollection
                ->addFieldToFilter('last_visit_at', $this->_localeDate->date($from, null, false)->format('Y-m-d H:i:s'));
    }
}

Note the code is not tested. Given base on the idea. In this sample, i have trying to get a collection of 7day old customer.

Also, note that Magento clear this table record automatically depends on the setting

1
  • after changing the customer to customer_id, It's working for me. Thank you for your valuable reply. – Sarvesh Tiwari Jun 5 at 11:09
0

Hi,

You can get the customer visitor data through this model.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerVisitorModel = $objectManager->create('Magento\Customer\Model\Visitor')->getCollection()->addFieldToFilter('customer_id', array('neq'=> ''));

Note: Don't use objectmanager. Get the model data through block or helper.

Let me know if you have any queries.

Thanks

2
  • but your answer is using objectManager – fmsthird Apr 16 '19 at 7:48
  • You can create the block to use this code. I tested this code in phtml file directly for you. – Moin Malek Apr 16 '19 at 7:52

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.