Solving Order Display Issues After Programmatic Creation in Magento 1.9.x
Are you facing challenges with displaying order details on the success page after programmatically creating an order via quote in Magento 1.9.x? You’re not alone, and I’ll guide you through a comprehensive solution to resolve this issue.
Understanding the Current Implementation
You’ve already implemented the createOrder() method that handles the order creation process. The code successfully submits the quote, sets the order status to ‘complete’, and saves the last order ID in the checkout session. However, when redirecting to the success page (checkout/onepage/success), the order details are not displayed.
Identifying the Root Cause
The problem lies in the redirection flow after a successful payment process through your custom payment method. Since you’re using an iframe for processing the payment, the standard checkout process is interrupted, causing the one-page checkout to fail in displaying the finalized order information.
Enhancing the Redirect Logic
To fix this issue, we need to modify the redirect logic in your PaymentController.php file. Let’s update the responseAction() method as follows:
public function responseAction()
{
// ... (existing code)
if ($resultCode === "Success") {
// ... (existing code)
$returnUrl = Mage::getUrl('checkout/onepage/success', array('_secure' => true, 'order_id' => $order->getIncrementId()));
} else
$returnUrl = Mage::getUrl('checkout/onepage/failure', array('_secure' => true));
// ... (existing code)
}
In this updated version, I’ve added the order increment ID as a parameter in the checkout/onepage/success URL using 'order_id' => $order->getIncrementId(). This ensures that the success page is loaded with the specific order information.
Modifying the One-Page Checkout Template
Next, we need to update the one-page checkout template to display the order details passed as a parameter. Open the success.phtml file located in your theme directory (usually under app/design/frontend/<your_package>/<your_theme>/template/checkout/).
Replace the existing content with the following:
<?php if ($this->getOrderId()): ?>
<div class="success-message">
<div class="page-title title-between-heading">
<h1><?php echo $this->__('Your order has been received') ?></h1>
<span>Order #<?php echo $this->getOrderId() ?></span>
</div>
<?php if ($this->getLastRealOrderId()): ?>
<p><?php echo $this->__('You can track the status of your order by following this link:') ?> <a href="<?php echo $this->getEscapedUrl('sales/order/view/reorder/false/order_id/' . $this->getLastRealOrderId()) ?>" target="_blank"><?php echo $this->getOrderId() ?></a></p>
<?php endif; ?>
<div class="box-tray">
<ul id="payment-methods" class="payment-methods">
<?php if ($this->getPaymentMethods()): ?>
<?php foreach ($this->getPaymentMethods() as $_code => $_method): ?>
<?php echo $this->getChildHtml($_code) ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div>
<?php else: ?>
<div class="success-message">
<p><?php echo $this->__('Your order cannot be retrieved.') ?></p>
</div>
<?php endif; ?>
I’ve added a conditional check to display the order details only when an order_id parameter is present in the URL.
Updating the One-Page Checkout Block
Finally, we need to update the one-page checkout block to pass the order ID from the URL parameters. Open the checkout_onepage_success.phtml file located in your theme directory (usually under app/design/frontend/<your_package>/<your_theme>/template/checkout/onepage/).
Replace the existing content with the following:
<?php if ($this->getOrderId()): ?>
<?php echo $this->getChildHtml('order_details') ?>
<?php else: ?>
<p><?php echo $this->__('Your order cannot be retrieved.') ?></p>
<?php endif; ?>
<div style="clear:both;"></div>
<p class="checkout-legend"><?php echo $this->__('You will be redirected to the homepage in %s seconds.', (int) Mage::getStoreConfig(Mage_Checkout_Model_Onepage::XML_PATH_ONEPAGE_HOME_REDIRECT)) ?></p>
Now, open the app/design/frontend/<your_package>/<your_theme>/layout/checkout.xml file and add the following block definition:
<block type="core/text_list" name="order_details" as="order_details">
<action method="setTemplate"><template helper="mymodule/data/getOrderDetailsTemplate"/></action>
</block>
Create a new helper file app/code/local/Mycompany/Mymodule/Helper/Data.php and add the following code:
public function getOrderDetailsTemplate()
{
$orderId = Mage::registry('current_order')->getIncrementId();
return "checkout/sales/order/view/info.phtml";
}
Conclusion
By implementing these changes, you should now have a functional solution that displays order details on the success page after creating an order programmatically in Magento 1.9.x.
Remember to replace <your_package> and <your_theme> with your actual package and theme names in the file paths mentioned above.
This comprehensive approach ensures that the order information is properly redirected, passed as a parameter, and displayed using the one-page checkout template. The use of helper functions enables dynamic template selection based on the current order.
For more advanced customizations or further assistance, feel free to explore our extensive Magento development services at https://laravelcompany.com. Our dedicated team of senior developers is always ready to help you overcome technical challenges and provide expert solutions tailored to your needs.