0

I am facing a rather strange issue with Magento 1.9. In the success page I have a piece of code that dynamically creates a tag based on the order just placed. The problem is that the tag is not showing up, although the comment just above it does. Here is the code I am using:

<!-- analytics -->
<?php $_order = Mage::getModel('sales/order')->loadByIncrementId( $this->getOrderId() ); ?>
<script type="text/javascript">
    sa('ecommerce', 'addOrder', JSON.stringify({
        order_id: '<?php echo $this->getOrderId(); ?>',
        revenue:  '<?php echo number_format($_order->getGrandTotal(), 2, '.' , ''); ?>',
        shipping: '<?php echo $_order->getShippingAmount(); ?>',
        tax:      '<?php echo $_order->getTaxAmount(); ?>'
    }));

    <?php foreach( $_order->getAllVisibleItems() as $item): ?>
        sa('ecommerce', 'addItem', JSON.stringify({
            order_id:   '<?php echo $this->getOrderId(); ?>',
            product_id: '<?php echo $item->getProductId(); ?>',
            name:       '<?php echo $item->getName(); ?>',
            price:      '<?php echo $item->getPriceInclTax(); ?>',
            quantity:   '<?php echo (int)$item->getQtyOrdered(); ?>'
        }));
    <?php endforeach; ?>
</script>

Weird thing is, if I remove the line just below the comment (the code that gets the order object), the script shows up with the expected errors. Also, if I add a line that prints the grand total for example, right after I get the order object, I get the grand total as expected... I see nothing in the log files and I set PHP to display all errors as well...

Also, note that there are other stuff below that piece of code that show up normally (buttons etc)...

Any ideas on why this is happening?

2
  • Can you try ->loadByIncrementId($this->getOrderId()); instead of ->loadByIncrementId( $this->getOrderId() ); – Sukumar Gorai Jul 27 '18 at 14:37
  • I highly doubt that spaces are the issue here. However, I have even tried loading a specific order id using ->load() – swiss_blade Jul 27 '18 at 18:31
0

I strongly believe that you are passing a wrong id to the object model, so your order object returns an empty array.

<?php $_order = Mage::getModel('sales/order')->loadByIncrementId( $this->getOrderId() ); ?>

Please make sure what you are getting from the $this->getOrderId()

This should be getting order Entity ID and your load method is for increment ID.

1
  • No unfortunately this is not the case since if I try something like $_order->getGrandTotal(), I get the correct grand total.. I have tried with the increment id however and the result is the same – swiss_blade Jul 30 '18 at 10:37

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.