我有這門課
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;
}
}
然后這個類被一個自定義控制器使用:
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');
}
}
}
在由我的支付網關觸發的 SECOND 自定義控制器上Callback,我使用$order = $this->getOrderFactory->create()->loadByIncrementId($incrementId)
我注入$this->getOrderFactory的一個實體在哪里。\Magento\Sales\Model\OrderFactory
我increment id從我的支付網關得到了回報。
不知何故,在這Callback堂課中,當我使用 時$order->getData('test'),我什么也得不到
我的問題是
我在這里缺少一些核心的magento概念嗎?
或者有沒有其他方法可以檢索這個Callback只有以下資訊的測驗資料increment Id(因為在回呼點,用戶已經離開 magento 并回來了)
這對我來說很奇怪,因為我可以編輯和保存訂單,Callback但我的額外資料沒有保存/與訂單物件本身關聯
提前致謝!
更新
我確認我使用order id從支付網關獲得的訂單物件(行)與從支付網關獲得的訂單物件(行)相同session's Last Order
我addStatusHistoryComment在lastOrder上面的 Api 類中呼叫并addStatusHistoryComment在我的 Callback 類中呼叫了兩個呼叫都在我的管理儀表板中更新相同的順序
我還確認getData('test')在我設定后立即呼叫它給我想要的資料。
所以我不明白為什么 getData 在從 Callback 呼叫時不起作用
uj5u.com熱心網友回復:
我最終使用了一個自定義鍵來setCustomerNote代替setData,這很奇怪,因為它確實在做:
return $this->setData(OrderInterface::CUSTOMER_NOTE, $customerNote);
我只能假設在 magento 2.4.x(這是我正在使用的順便說一句)上,setData僅限于預定義的鍵。
uj5u.com熱心網友回復:
您不能只將資料添加到訂單物件,每個模型都會自動映射到資料庫表列,物件將臨時存盤資料并且不會出錯,但它不會保留在資料庫中。注釋之所以起作用,是因為它們在資料庫中占有一席之地,并且在保存和加載時,有額外的代碼可以保存并將其添加到模型中。
為了在訂單中保留新資料,您需要添加新的訂單屬性或簡單地在銷售訂單表上添加新列。保存時,鍵與您創建的新列的名稱完全匹配。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443632.html
標籤:php 马金托 magento2 magento2.4
