0
votes

I'm struggling with a special little problem related to the redirect out of some Magento observer.

I wrote an extension and put an observer to the "checkout_submit_all_after" event, which works out just fine. My little extension automatically creates an invoice and sets the order status to processing, once the payment method is "Invoice". Unfortunately the redirect after submitting an order in the one page checkout doesn't work anymore. It always redirects to "checkout/cart" instead of "checkout/onepage/success".

Someone any ideas what I'm doing wrong?

Here's my code:

class Shostra_AutoInvoice_Model_Order_Observer
{
    public function __construct()
{

}


public function auto_create_invoice($observer)
{

    $order = $observer->getEvent()->getOrder();

    if (!$order->hasInvoices()) {
        $payment = $order->getPayment()->getMethodInstance()->getTitle();
        Mage::log("payment method: " . $payment);

        if($payment=="Rechnung"){
            Mage::log("autocreating invoice");
            $invoice = $order->prepareInvoice();
            $invoice->register();
            $invoice->pay();
            $invoice->save();
            $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
            Mage::log("invoice created and saved");
        }
        $this->addComment('Order automatically set to paid.');
    } else {
        $this->addComment('no invoices found.');
    }

    $response = $observer->getResponse();
    $response->setRedirect(Mage::getUrl('checkout/onepage/success'));
    Mage::getSingleton('checkout/session')->setNoCartRedirect(true);
}
}

Thanks a lot!

1

1 Answers

1
votes

Why don't you try sales_order_save_after event and try saving it, so no issues with redirection manually as Magento will take its own course,

you can refer to this link for more explanation

http://inchoo.net/magento/magento-orders/automatically-invoice-ship-complete-order-in-magento/