0

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!

4
  • What type of payment method are you using. If your payment method set your order status to 'processing' then it should get auto invoiced. docs.magento.com/user-guide/sales/order-status.html Commented Jun 29, 2022 at 11:32
  • It depends on the marketplace the order comes from. Most of the orders in question are from third-party marketplaces which are integrated with our Magento stores. Our third-party marketplaces don't receive automatic invoices, and instead are currently processed manually. Commented Jun 29, 2022 at 11:42
  • What interal logic do you use to figure out when the order can be invoice? Commented Jun 29, 2022 at 11:57
  • Usually we send an invoice when we dispatch the order, however this doesn't have to be the case in this instance. Commented Jun 29, 2022 at 13:01

1 Answer 1

0

To auto invoice an order try:

public function fullPaymentReceivedAutoInvoice($order){
    $invoice = $order->prepareInvoice();

    if($invoice->getTotalQty()){
        $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
        //$invoice->getOrder()->setCustomerNoteNotify(false);
        $invoice->register();
        Mage::getModel('core/resource_transaction')
                ->addObject($invoice)
                ->addObject($invoice->getOrder())
                ->save();

        $invoice->sendEmail(false); 
        
        if($order->getState() ==  Mage_Sales_Model_Order::STATE_NEW){
            $order->setStatus('processing');

            $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING);
            $order->save();
        }
        
     }
}
1
  • Would you append this to an existing file or are you suggesting using this as a standalone extension? Commented Jun 29, 2022 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.