0

I'm trying to get access to the grand total of the order but for some reason am unable to access this variable.

I have tried multiple solutions by creation modules but nothing seems to be working.

success.phtml

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>

<!---- Onefeed Tracking Code ---->
<?php
//-------------------------------------------
// GET MAGENTO 2 ORDER VALUES FOR TRACKING CODE
//-------------------------------------------
$orderId = $block->getOrderId();
echo $block->getOrder();
$total = 2;
?>

<script type="text/javascript">
  var of_ssid = '362-1';
  var cs = '<?php echo $total; ?>';
  var oi = '<?php echo $orderId; ?>';
  var it = 1;
  var id = '';
  var pairs = document.cookie.split("; "),
    count = pairs.length, parts;
  while (count--) {
    parts = pairs[count].split("=");
    if (parts[0] === "user" && parts[1] === "usr")
      id = parts[2];
  }
</script>
<script type="text/javascript" language="javascript" src="https://tracking.onefeed.co.uk/t.js"></script>
<noscript>
    <img border="0" src="https://tracking.onefeed.co.uk/ProcessProductCheckout.ashx?of_ssid=362-1&cs=<?php echo $total; ?>&it=1&oi=<?php echo $orderId; ?>" />
</noscript>

<!---- End Onefeed Tracking Code ---->

success.php

<?php

namespace Creation\OrderSuccess\Block;

class Success extends \Magento\Checkout\Block\Onepage\Success {

    public function getOrder() {
        return $this->_checkoutSession->getLastRealOrder();
    }

}

echo $block->getOrder() outputs null, if I echo $block->getGrandTotal() I also get null.

I can't seem to figure out where I'm missing the connection to access this data.

  • Can i assume that when your testing you are refreshing the page? if so when the page is refreshed that data is removed to prevent this on a temporary basis i never recommend editing the core files on a permenant basis only for quick testing. go to vendor/magento/module-checkout/Controller/Onepage/Success.php on line 22 comment out $session->clearQuote();which will prevent the checkout from clearing itself so you can do proper test – Dava Gordon Aug 21 at 13:34
  • Yeah you are correct although I have already tried this and still wasn't getting the data I needed. Are you able to access the quote inside of success.php? – Howliee Aug 21 at 13:57
  • I am able to retreive the last order id and then load that via my own block i never extend the success block as i prefer my code to be independand of core blocks as magento is constantly changing class names and depricating things – Dava Gordon Aug 21 at 14:14
  • Do you have an example I would be able to take reference from? – Howliee Aug 21 at 14:54
  • please see answer below – Dava Gordon Aug 21 at 15:14
0

Something like this should work no issue tried and tested.

<?php

namespace Vendor\Module\Block\Checkout;

use Magento\Checkout\Model\Session;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\OrderFactory;

class Success extends \Magento\Framework\View\Element\Template
{

    protected $scopeConfig;

    protected $checkoutSession;

    protected $orderModel;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,  
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Checkout\Model\Session $checkoutSession,       
        \Magento\Sales\Model\Order $orderModel,
        array $data = []        
    ) {
        parent::__construct($context, $data);       
        $this->_scopeConfig = $scopeConfig;
        $this->_checkoutSession = $checkoutSession;
        $this->_orderModel = $orderModel;
    }

    public function getRealOrderId()
    {
        $lastorderId = $this->_checkoutSession->getLastOrderId();
        return $lastorderId;
    }   

    public function getOrder() {

        $orderId = $this->getRealOrderId();
        $_order = $this->_orderModel->loadByIncrementId($orderId);

        return $_order;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.