I’ve an e-commerce website powered by Magento but now I need to make the connection bewteen Magento and the company’s management software.
The process should be this:
1. The user put some stuff in his cart
2. The user go through the checkout process
3. After the user have paid the total with Paypal, Magento should send an HTTP request (perhaps POST, with order’s data) to an external server that handles the request and do stuff with the data received.
My problem is that I’ve no idea how to send a request from Magento with the order data after the checkout process. I think this is a common scenario for companies that uses e-commerce. Do you have some ideas for this? Thanks.
For such purposes it is usually used success page that appears after successful checkout
The standard page location is app/design/frontend/default(base)/default/template/checkout/success.phtml.
It’s the easiest way to collect information about existing orders from this file and pass it to remote server by curl (for ex.)
The information about the orders for Google Analytics is collected and sent from this file too.
Simple code (as example очень простой код (only as an example of implementation and not a guide as it should be):
In the template in the begin of success.phtml we add a code:
<?php $this->sendOrder(); ?>
in Mage_Checkout_Block_Onepage_Success we add the method itself:
public function sendOrder()
{
$remote_serv_url = \'http://www.myremoteserver.com/order_registration_controller/?orderinfo=\'; //url of remote server with controller $param = json_encode(Mage::getModel(\'sales/order\')->getCollection()->addFieldToFilter(\'increment_id\', $this->getOrderId())->getLastItem()->getData()); //^^^^^^^^collecting of data for recieve
$url = $remote_serv_url . $param;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$success = curl_exec($ch); //recieving of success from remote server
}
Yes, in case of a big quatntity (really very big quantity) of orders it may be increased the load on the server. In this case it is possible to send data about orders via Cron (the accumulation of a number of them over time and sending all at once with one request). It may slightly reduce the load on the server.
For a very large flow of orders (for very large stores) the best variant of transfering data of the orders is through the API.
The presented implementation is the most simple and in original the question was how to organize data transfer directly after placing the order