How to Set and Get Data Using Data Persistor Dynamically in Magento 2

 Steps to Set and Get Data Using Data Persistor Dynamically in Magento 2:

Step 1: First, create the Helper file at the given below path


app/code/Vendor/Extension/Helper/Data.php


Then add the code as follows


<?php

namespace Vendor\Extension\Helper;

 

use Magento\Framework\App\Helper\AbstractHelper;

use Magento\Framework\App\Helper\Context;

use Magento\Store\Model\ScopeInterface;

use Magento\Framework\App\Request\DataPersistorInterface;

 

class Data extends AbstractHelper

{

    public function __construct(

        Context $context,

        DataPersistorInterface $dataPersistor)

    {

        $this->dataPersistor = $dataPersistor;

        parent::__construct($context);

    }

 

    public function setValue($parameter,$value)

    {

        $this->dataPersistor->set($parameter, $value);

    }

    public function getValue($parameter)

    {

        echo $this->dataPersistor->get($parameter);

    }

    public function ClearValue($parameter)

    {

        $this->dataPersistor->clear($parameter);

    }

 

}

Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Update Product Stock Programmatically in Magento 2

How to Set and Get Cookie in Magento 2