skip to Main Content

I have this class

class Api extends MagentoFrameworkModelAbstractModel
{
 public function __construct(
        MagentoFrameworkMessageManagerInterface $messageManager,
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        MagentoStoreModelStoreManagerInterface $storeManager,
        MyModulePaymentHelperData $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 MagentoFrameworkAppActionAction
{
    public function __construct(
        MagentoFrameworkAppActionContext $context,
        MyModulePaymentModelApi $moduleApi,
        MagentoCheckoutModelSession $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 MagentoSalesModelOrderFactory 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 beause 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!

UPDATE

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

I called addStatusHistoryComment on lastOrder in Api class above and also called addStatusHistoryComment in 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

2

Answers


  1. Chosen as BEST ANSWER

    I ended up using setCustomerNote in place of setData with a 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.


  2. You can’t just add data to order object, every model is mapped automatically to DB tables columns, object will store temporarily the data and not error out but it will not persist in database. The reason why comments work, is because they have a place in database and on save and on load there is extra code that saves and adds it to the model.

    In order to persists new data in the order you need to add new order attribute or simply add a new column on sales order table. When saving, key much match exactly the name of the new column you created.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search