如何在magento 2.3.3中获得grandTotal?

如何在magento 2.3.3中获得grandTotal?,magento,return-value,payment-gateway,Magento,Return Value,Payment Gateway,我正在使用magento 2.3.3,并尝试创建自定义支付网关。当我试图从cart或total获取grandTotal值时,出现了此问题。我尝试了很多不同的代码。有人能帮我吗?作为一个例子,我将展示一些我尝试过的解决方案 $objectManager=\Magento\Framework\App\objectManager::getInstance(); $cart=$objectManager->get('\Magento\Sales\Model\Order'); $grandTotal=$c

我正在使用magento 2.3.3,并尝试创建自定义支付网关。当我试图从
cart
total
获取grandTotal值时,出现了此问题。我尝试了很多不同的代码。有人能帮我吗?作为一个例子,我将展示一些我尝试过的解决方案

$objectManager=\Magento\Framework\App\objectManager::getInstance();
$cart=$objectManager->get('\Magento\Sales\Model\Order');
$grandTotal=$cart->getGrandTotal()

此代码与其他代码一样返回NULL。任何帮助都将不胜感激

我已经创建了控制器文件,将客户重定向到支付网关表单。它很好用。但我必须寄一笔钱。这是代码的一部分

 public function execute(){
 $back_url = $this->urlBuilder->getUrl('tarlanpay/redirect/callback');
    $store_id = $this->getStoreId();
    $order_id = $this->_checkoutSession->getData('last_order_id'); 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $cart = $objectManager->get('\Magento\Checkout\Model\Cart');
    $grandTotal = $cart->getQuote()->getGrandTotal();
    $test_mode = $this->scopeConfig->getValue('payment/tarlanpay/test_mode', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $merchant_id = $this->scopeConfig->getValue('payment/tarlanpay/merchant_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $encrypted_key = $this->scopeConfig->getValue('payment/tarlanpay/secret_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $secret_key = $this->_encryptor->decrypt($encrypted_key);
    // var_dump($grandTotal) ;die(); // it is debugging

    $post_data = [
            'reference_id' => $store_id.'-'.$order_id,
            'amount' => $grandTotal,
            'description' => 'magento 2',
            'merchant_id' => $merchant_id,
            'secret_key' => $secret_key,
            'is_test' => $test_mode,
            'back_url' => $back_url,
            'request_url' => 'http://magento2'
    ];
   $post_data['secret_key'] = password_hash($store_id.'-'.$order_id.$secret_key, PASSWORD_BCRYPT, ['cost' => 10]);

    $ctp_url = 'there is an API.examlpe.com';

    $curl = curl_init($ctp_url);

    curl_setopt($curl, CURLOPT_HTTPHEADER, array (
     'Accept: application/json'
    ));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

    $response = curl_exec($curl);

    $decoded_response = json_decode($response, true);

    $form_action_url = $decoded_response['data']['redirect_url']; //it's an info that came from payment gateway
    $array_data = array (
    'action' =>$form_action_url,
    'fields' => 'success'
    );
    $result = $this->resultJsonFactory->create();
    return $result->setData($array_data);
}
试试这个代码

 public function execute(){
 $back_url = $this->urlBuilder->getUrl('tarlanpay/redirect/callback');
    $store_id = $this->getStoreId();
    $order_id = $this->_checkoutSession->getData('last_order_id'); 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($order_id);
    $grandTotal = $order->getGrandTotal();
    $test_mode = $this->scopeConfig->getValue('payment/tarlanpay/test_mode', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $merchant_id = $this->scopeConfig->getValue('payment/tarlanpay/merchant_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $encrypted_key = $this->scopeConfig->getValue('payment/tarlanpay/secret_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $secret_key = $this->_encryptor->decrypt($encrypted_key);
    // var_dump($grandTotal) ;die(); // it is debugging

    $post_data = [
            'reference_id' => $store_id.'-'.$order_id,
            'amount' => $grandTotal,
            'description' => 'magento 2',
            'merchant_id' => $merchant_id,
            'secret_key' => $secret_key,
            'is_test' => $test_mode,
            'back_url' => $back_url,
            'request_url' => 'http://magento2'
    ];
   $post_data['secret_key'] = password_hash($store_id.'-'.$order_id.$secret_key, PASSWORD_BCRYPT, ['cost' => 10]);

    $ctp_url = 'there is an API.examlpe.com';

    $curl = curl_init($ctp_url);

    curl_setopt($curl, CURLOPT_HTTPHEADER, array (
     'Accept: application/json'
    ));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

    $response = curl_exec($curl);

    $decoded_response = json_decode($response, true);

    $form_action_url = $decoded_response['data']['redirect_url']; //it's an info that came from payment gateway
    $array_data = array (
    'action' =>$form_action_url,
    'fields' => 'success'
    );
    $result = $this->resultJsonFactory->create();
    return $result->setData($array_data);
}

它也返回NULL。我发现了一件有趣的事。当我在中单击“下订单”按钮时显示为空,但当我并行打开第二页并将产品添加到购物车(在第二页)并刷新第一页时,它会显示金额(在第一页)。这意味着,当我触发“下订单”按钮在清洁购物车。如何禁用?我可以知道你如何创建自定义支付网关吗?我已经编辑了我的问题。如果你需要更多信息,请告诉我。请检查我的答案。我已经更新了它。如果它解决了你的问题,请接受它。你的答案完全正确!只需尝试删除
echo
here->`$grandTotal=echo$order->getGrandTotal();`一切正常!非常感谢你!