0

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(
    array(
      'label' => 'Tela',
      'value' => 'tela02'
    )
 }

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

Arshad Hussain
  • 906
  • 5
  • 18
  • 38

1 Answers1

0

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.

Edit:

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:

https://magento.stackexchange.com/a/94267/48355

PЯINCƎ
  • 11,669
  • 3
  • 25
  • 80