essentially as the title states, I want to create automatic invoicing for orders in Magento 1. I've considered a few potential solutions to this which I'll go into below, but was hoping for some advice/feedback on the ideas, and if anyone has done something similar before I'd appreciate any insight you can offer.
Important note: I need to be able to change how we send the invoice depending on the marketplace the order comes from. E.g. for one marketplace, we may wish to directly send an email. For another marketplace, we may need to send an API call with our invoice and some relevant headers. For this reason, it's important for us to be able to distinguish orders between marketplaces and have a different thing happen for each.
So, here's what I've considered so far:
1 - Magento 1 Extension. Is there an extension that can possibly handle this? I've seen some which can do automatic invoicing, but they'd usually not be able to distinguish between marketplaces and allow us to make an API call as required by some, which is a sticking point. Any advice on this is appreciated.
2 - Modifiying/replicating the getPDF Method. Within Magento 1, there's already the getPDF method which is utilised when you click "Print" on an invoice. I don't really wish to modify core code, but I thought perhaps it's possible to create a similar observer to that in point 3, and then call a similar, slightly modified, getPDF method which would save the PDF to some folder in the root directory. These could then be checked by an external script which would do the remaining processing and send the invoices off.
3 - Custom php observer. I've seen this utilised for similar uses in other forums and figured it could work. Essentially the observer would catch any new incoming order, gather the required data for the order and then send it to an external webserver via curl for processing. On this webserver, I could create python scripts which will generate the PDF invoice and send it in the relevant manner depending on the marketplace. An example of what this code could look like can be seen below, but I'm not sure the feasibility or reliability of this method and would appreciate some feedback:
class Automatic_Postdata_Model_Checkout_Observer
{
public function postDataToServer(Varien_Event_Observer $observer){
$orderIds = $observer->getData('order_ids');
foreach($orderIds as $_orderId){
$order = Mage::getModel('sales/order')->load($_orderId);
$customer = Mage::getModel('customer/customer')->load($order->getData('customer_id'));
$customer->getDefaultBillingAddress()->getLastname();
$billingaddress = $order->getBillingAddress();
try { /* parameters can be changed according to requirment */
$params = array( 'customerName'=>$order->getData('customer_firstname'),
'companyName'=>$billingaddress->getData('company'),
'telephone'=> $billingaddress->getData('telephone'),
'email'=> $billingaddress->getData('email'),
'street'=> $billingaddress->getData('street'),
'city'=> $billingaddress->getData('city'),
'region'=> $billingaddress->getData('region'),
'postcode'=> $billingaddress->getData('postcode'),
'total'=>$order->getGrandTotal() );
$url= 'example.com/getorders.php'; // the url to send data to via curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HEADER);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$response = curl_exec($ch);
curl_close($ch);
Mage::log('Order has been sent to example.com');
} catch (Exception $e) {
Mage::logException($e);}
}
return $this;
}
} ?>
Thanks in advance!