2

I need a way to set the order status automatically to completed when the order is created in admin and payment method is one of the two possibilities for admin orders. In this case it is unnecessary step to create invoice and shipment.

Already tried this, but could not get it to work:

http://www.atwix.com/magento/auto-invoice-and-custom-order-status-upon-checkout/

0

follow this tutorial

We need to add following code in config.xml

<sales_order_place_after>
    <observers>
        <b4u_order_observer>
            <type>singleton</type>
            <class>Mshop_B4u_Model_Observer</class>
            <method>after_order_placed</method>
        </b4u_order_observer>
    </observers>
</sales_order_place_after>

Need to write the following code in observer( mshop -> b4u -> model -> Observer.php )

public static function after_order_placed($observer)
{
    $event = $observer->getEvent();
    $order = $event->getOrder();
    $order_no   = (string) $order->getRealOrderId();

    $order->loadByIncrementId($order_no);
    $order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true);
    $order->save();
    return true;
}

And we can create invoice also through this script. for that we need to add extra code and the above function should look like below,

public static function after_order_placed($observer)
{
    $event = $observer->getEvent();
    $order = $event->getOrder();
    $order_no   = (string) $order->getRealOrderId();

    // **** This is for creating invoice *****
    $order = Mage::getModel('sales/order')->loadByIncrementId($order_no);
    try {
        if(!$order->canInvoice()) {
            Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
        }

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

        if (!$invoice->getTotalQty()) {
            Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
        }

        $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
        //Or you can use
        //$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);

        $invoice->register();
        $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($invoice)
            ->addObject($invoice->getOrder());

        $transactionSave->save();
    } catch (Mage_Core_Exception $e) {
    }

    // **** This is for set as order complete *****
    $order->loadByIncrementId($order_no);
    $order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true);
    $order->save();
    return true;
}
  • I did not yet try this solution, but I also need it only to work with certain payment methos that we use only in admin. One is Check / money (checkmo) other is a custom payment method. It should not auto-complete an order when used with payment method that is in use in frontend. – Webninja Oct 20 '15 at 16:42
  • $order()->getPayment->getMethodInstance(); you can check payment method by this code and put your condition.. – Qaisar Satti Oct 22 '15 at 5:08

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.