Skip to content

Set function not working on CustomerVisitor model for custom column

I am trying to add new custom columns for now online customer grid.

I have added new custom columns in customer_visitor table, but I am not able to save data in custom columns.

Firstly, I have added new custom columns in customer_visitor table with db_schema.xml:

<?xml version="1.0"?>

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="customer_visitor" resource="default">
        <column xsi:type="varchar" name="device_type" length="20" nullable="true" comment="Device Type"/>
        <column xsi:type="varchar" name="last_visited_page" length="255" nullable="true" comment="Last Visited Page (End Point URL)"/>
        <column xsi:type="varchar" name="last_visited_product" length="255" nullable="true" comment="Last Visited Product SKU"/>
    </table>
</schema>

But when I tried to add value to these columns with ‘set() function’ then the value of default column is saved, but the value of custom column is not getting saved. Showing NULL

Below I have shown with code and image, please check:

public function __construct(
   MagentoCustomerModelVisitorFactory $visitor,
   MagentoCustomerModelResourceModelVisitor $resourceModel
) {
   $this->visitor = $visitor;
   $this->resourceModel = $resourceModel;
}

public function createVisitor() {
   $visitorModel = $this->visitor->create();
   $this->resourceModel->
   load($visitorModel,'','visitor_id');
   $visitorModel->setCustomerId(0);
   $visitorModel->setCreatedAt("2022-09-16 12:10:39");
   $visitorModel->setLastVisitAt("2022-09-16 12:10:39");
   $visitorModel->setDeviceType("device type");
   $visitorModel->setLastVisitedPage("last page");
   $visitorModel->setLastVisitedProduct("product");
   $this->resourceModel->save($visitorModel);
        
   return $visitorModel;
}

enter image description here
enter image description here

I think this is a correct way, but it is not working. I don’t know where I am wrong.
I am using Magento2.4.5.

What can I do to add value in custom column?

Thank You!