I'm trying to send a variable from a phtml to an observer through an event observer. Is that possible?

Note: variables are defined by a script in the phtml.

Edit: The name of event is "checkout_cart_product_add_after". In the script I have some like this:

function cambio_telas(event){
    telaactual = event.data.tela;

And I want to send the var "telaactual" to the observer:

public function execute(\Magento\Framework\Event\Observer $observer) {

  $item = $observer->getEvent()->getData('quote_item');
  $item = ( $item->getParentItem() ? $item->getParentItem() : $item );

  $additionalOptions = array(
      'label' => 'Tela',
      'value' => 'tela02'

In short, I need to apply the variable "currentdata" to value in "$ aditionalOptions"

  • Please add some more information like which event, variable. Also add script to get clear idea. – Abhishek Panchal Aug 25 '17 at 21:21
  • thx for you recomendation. – Andrés Felipe López Aug 25 '17 at 21:39

You can use getData() and setData()

in phtml :

$myVal = 'hello';

Mage::getSingleton('core/session')->setData('value1', $myVal); //save data in value1

in Observer :

Mage::getSingleton('core/session')->getData('value1'); //you get the saved value in value1
Mage::getSingleton('core/session')->unsetData('value1'); // you empty the value1 value from session for the next use.


Sorry, I didn't see that you want the Magento 2, there was no tag, however I'm not Magento 2, but you can follow this tuto:


| improve this answer | |
  • Hi this works for magento 2.1??? .. i need some configuration?? when i use Mage::getSingleton('core/session')->setData('value1', $myVal); //save data in value1 the page crash. – Andrés Felipe López Aug 27 '17 at 19:15
  • Please, look my update – PЯINCƏ Aug 27 '17 at 19:39
  • :) nice, now i have defined ´function getCheckoutSession()´ in my block, but ´$this->getCheckoutSession()->getMyValue();´ no work in observer... I think I missed something but I do not know. – Andrés Felipe López Aug 27 '17 at 20:21

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.