0

Recently I have migrated our website. I have imported the orders however they all by default were changed to complete and no invoice was created.

The main problem is that it works great with one order number, more order numbers comma-delimited get me the PHP error.

Parse error: syntax error, unexpected ',' in /home/alphalasulaco/public_html/inv-cre.php on line 5

I have compiled the following script to change the order to processing and allow invoice creation.

<?php
require_once('./app/Mage.php');
Mage::app();

$orderNumbers = 100124858,100124857;

$order = Mage::getModel("sales/order")->load($orderNumbers);

$collection = Mage::getModel('sales/order')->getCollection()
                ->addFieldToFilter('increment_id', array('in' => $orderNumbers));

foreach ($collection as $order) {
            $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING)
                ->setStatus(Mage_Sales_Model_Order::STATE_PROCESSING)
                ->save();
}

try {
if($order->canInvoice())
{ 
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

if ($invoice->getTotalQty()) { 

$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();
$transaction = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder()); 
$transaction->save();
Mage::getSingleton('core/session')->addSuccess('Invoice Created Successfully.'); 
}else{
 Mage::getSingleton('core/session')->addError('Can not create invoice without Product Quantities'); 
 }

}else{
Mage::getSingleton('core/session')->addError('Can not create invoice'); 
}
}
catch (Mage_Core_Exception $e) {
 echo $e->getMessage();
}
?>
0

Well, the error gives it away:

$orderNumbers = 100124858,100124857;

Should be:

$orderNumbers = array(100124858,100124857);

Then to get your code working you need to delete the following line:

$order = Mage::getModel("sales/order")->load($orderNumbers);

And move the entire try/catch code block inside the collection foreach loop.

  • Would you know how to force invoice number to be the same as the order number, using the above script? – damek132 Feb 26 '16 at 12:05
  • @damek132 well that's a different issue, see here: stackoverflow.com/questions/11965721/… – Raphael at Digital Pianism Feb 26 '16 at 12:06
  • Right, it came to my attention that there is a extension which keeps the order number as invoice number, shipment number and credit memo number. I am hoping that with that plugin I will be able to achieve this using the script. – damek132 Feb 26 '16 at 12:24
  • @damek132 feel free to upvote and mark your question as answered if my post helped you in your original question. – Raphael at Digital Pianism Feb 26 '16 at 12:25

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.