我们在Magento 2中遇到了一些问题,无法在结账代码中输出订单值。这是我们的代码:
<!---- Onefeed Tracking Code ---->
<?php
//-------------------------------------------
// GET MAGENTO ORDER VALUES FOR TRACKING CODE
//-------------------------------------------
$orderId = $block->escapeHtml($block->getOrderId());
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->load($orderId);
$total = $order->getGrandTotal();
?>
<script type="text/javascript" language="javascript">
var of_ssid = 'xxxx';
var cs = '<?php echo $total ?>';
var oi = '<?php echo $orderId; ?>';
var it = 1;
</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=xxxx&cs=<?php echo $total ?>&it=1&oi=<?php echo $orderId; ?>" />
</noscript>
<!---- End Onefeed Tracking Code ---->
有什么想法吗?我们如何在不必在Magento 2中创建模块的情况下输出数据?
您的代码的这一部分可以确定地工作(刚刚检查(:
<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$order = $objectManager->create('MagentoSalesModelOrder')->load($orderId);
$total = $order->getGrandTotal();
?>
...
<?php echo $total ?>
由于我不知道您所依赖的Block函数,因此无法确定,但我猜$orderId = $block->escapeHtml($block->getOrderId());
不会返回有效的订单id。
不过,我不确定您是否应该使用objectManager。关于何时以及如何在stackexchange:上使用它,有很多讨论
https://magento.stackexchange.com/questions/117098/magento-2-to-use-or-not-to-use-the-objectmanager-directly