cancel
Showing results for 
Search instead for 
Did you mean: 

How to setData and getData between controllers

How to setData and getData between controllers

I have this class

 

class Api extends \Magento\Framework\Model\AbstractModel
{
   public function __construct(
   \Magento\Framework\Message\ManagerInterface $messageManager,
   \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
   \Magento\Store\Model\StoreManagerInterface $storeManager,
   \MyModule\Payment\Helper\Data $helper
   ) {
     $this->messageManager = $messageManager;
     $this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager; $this->helper = $helper; $this->contentType = $this->helper->getConfigData('content_type'); } . . . function createOnlinePurchase($authToken, $lastOrder) { . . . //here I pass lastOrder's increment id to my payment gateway $lastOrder->setData('test','test data'); $lastOrder->save(); //make payment gateway api call, get payment url return url; } }

 

 

this class is then used by a custom controller:

class Index extends \Magento\Framework\App\Action\Action
{
   public function __construct(
   \Magento\Framework\App\Action\Context $context,
   \MyModule\Payment\Model\Api $moduleApi,
   \Magento\Checkout\Model\Session $session
   ) {
     parent::__construct($context);

     $this->moduleApi = $moduleApi;
     $this->session = $session;
   }

   public function execute() {
     $token = $this->moduleApi->requestAuthToken();

     if ($this->session->getLastRealOrder() && $token !== null) {
     $url = $this->moduleApi->createOnlinePurchase($token, $this->session->getLastRealOrder());

     if ($url !== null && substr($url, 0, 4) == 'http') {
       $this->session->clearStorage();
       return $this->resultRedirectFactory->create()->setUrl($url);
     }
     else {
       $this->messageManager->addError(__("Url invalid: ".$url));
       return $this->resultRedirectFactory->create()->setPath('checkout/onepage/failure');
     }
   }
}

 

on a SECOND custom controller Callback, which is triggered by my payment gateway, I retrieved the order object using 

$order = $this->getOrderFactory->create()->loadByIncrementId($incrementId)

where 

$this->getOrderFactory

is an instance of \Magento\Sales\Model\OrderFactory I injected.

 

I got the increment id back from my payment gateway.

Somehow within this Callback class, when I use

$order->getData('test')

I get nothing

 

My question is

Is there some core magento concept I'm missing here?

Or is there any other way I can retrieve this test data in Callback which only have the information of increment Id (because at the point of Callback, user have already left magento and come back)

It's weird to me because I can edit and save the order from Callback but my extra data is not saved/associated with the order object itself

Thanks in advance!

1 REPLY 1

Re: How to setData and getData between controllers

UPDATE

I confirmed that I'm getting the same order object(row) by using the order id I get from my payment gateway and the one from session's Last Order 

 

I called addStatusHistoryComment on lastOrder in Api class above and also called addStatusHistoryComment on my Callback class

 

Both calls are updating the same order in my admin dashboard I have also confirmed calling getData('test') right after I set it gives me the data I want.

So I don't understand why getData doesn't work when called from Callback

 

MY SOLUTION

I ended up using setCustomerNote in place of setData with custom key

which is weird that it works because it is literally doing:

return $this->setData(OrderInterface::CUSTOMER_NOTE, $customerNote);

I can only assume on magento 2.4.x (which is what i'm using btw), setData is restricted to predefined keys only

 

Please let me know if this way of doing thing is correct or should I avoid doing this?

 

I also just found out I can use 

setCustomAttributes for multiple data, since order extends AbstractExtensibleModel