0

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(
   \Magento\Customer\Model\VisitorFactory $visitor,
   \Magento\Customer\Model\ResourceModel\Visitor $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!

1 Answer 1

0

When saving the data in a table in Magento way you should use the model and resource model for this, below is the following class for the resource model -

Magento\Customer\Model\ResourceModel\Visitor

Now below is the sample code for saving data in a table using the model and resource model.

The main thing is you can create a factory for the model and set the data using setters(set()) and getters(get()) and load them up in the resourceModel using the load() function but you have to pass 3 arguments ->

  1. modelFactory instance
  2. primary key value (If this is auto increment then just give like this = '')
  3. pass the primary column name.

Sample code

public function __construct(
   \Magento\Customer\Model\VisitorFactory $visitor,
   \Magento\Customer\Model\ResourceModel\Visitor $resourceModel
) {
   $this->visitor = $visitor;
   $this->resourceModel = $resourceModel;
}

public function createVisitor() {
   $visitorModel = $this->visitor->create();
   $this->resourceModel- 
   >load($visitorModel,'','primary_key_column_name');
   $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;
}

Hope this posts helps , you can always comment back if any more help is needed , please do give a like if this helped.

2
  • I tried as you mentioned, but it's not working. I have updated Question, please check. Sep 16, 2022 at 14:02
  • Hi @MukeshKhatri Do you get any errors because of this , can you try using try catch statements and share the error here ? Sep 18, 2022 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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