0

I have a form that the visitor fills in and based on that form submission (to a controller) I need to save some data in a custom section. I have my custom section working correctly but only returning a simple hard coded string currently:

<?php
namespace MyCompany\MyModule\CustomerData;
use Magento\Customer\CustomerData\SectionSourceInterface;

class CustomSection implements SectionSourceInterface
{
    public function getSectionData()
    {
        // this saves to the section but how to make this dynamic based on the form submission values?
        return [
            'customdata' => "Custom data",
        ];
    }
}

So my question is how do I save data to my custom section from a controller? Thanks.

2
  • Can you provide some more info like is the data submitted on user wise or its just a normal form like contact us and you need to save that data in database and showed that data in Magento Admin ?
    – Prits
    Oct 7 at 14:06
  • It's a "filtering" form (to filter products on the category page). I have it working now though (using sessions). I will post my answer. Thanks.
    – sulman
    Oct 7 at 15:04
0

I have got this working by saving the posted form data in a session then returning that session data in the getSectionData() method:

<?php
namespace MyCompany\MyModule\CustomerData;
use Magento\Customer\CustomerData\SectionSourceInterface;

class CustomSection implements SectionSourceInterface
{

    public function __construct(\MyCompany\MyModule\Helper\Session $session)
    {
        $this->session = $session;
    }


    public function getSectionData()
    {
        return [
            'customdata' => $this->session->getMySession(),
        ];
    }
}

This doesn't seem right but it works and I am potentially only working with temporary data (I only save it to a model if customer is logged in)

p.s. The main reason I am using private data instead of directly displaying cookies/session data is to avoid Magento caching personalized data.

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.