Get order number in plugin after order is placed

Hi,

I’m trying to get the order number after an order is placed in my shop to send that to a different place the problem is I can’t find the correct event to get this. I’ve been trying with ‘Enlight_Controller_Action_Frontend_Checkout_Finish’ and ‘Shopware_Modules_Order_SaveOrder_ProcessDetails’ but sOrderNumber doesn’t seem to be defined at that point. Is there any other event to which I can subscribe to?

Thanks

Hi,

have you tried using the same way the core gets the ordernumber?

It should be in the Session at that time. Try something like:

$sOrder = $this->container->get('session')->offsetGet('sOrderVariables');

Otherwhise Shopware_Modules_Order_SendMail_FilterVariables has the ordernumber too.

Hi [@Moritz Naczenski](http://forum.shopware.com/profile/14574/Moritz Naczenski “Moritz Naczenski”)‍

Yes I’m using something like this (and after your comment tried exactly that) but still not getting the order number, it’s not defined in sOrderVariables but if I refresh the page and resend the information the order number is there, so I think it’s because I’m using Enlight_Controller_Action_Frontend_Checkout_Finish and the order number is not saved yet?

You can try get the order number with  Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout_Finish,  or maybe you can look working with the session:

$this->session = Shopware()->Session();
$sOrderVariables = $this->session[‘sOrderVariables’]->getArrayCopy()

In this way you have complete order variables where it is also included sOrderNumber.

I must be doing something really wrong because still can’t see the order number. Let me share some code with you guys and see, this is part of my code

public static function getSubscribedEvents()
  {
      return [
          'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout_Finish' => 'onProcessDetails'
      ];
  }




public function onProcessDetails(\Enlight_Event_EventArgs $args)
{
      
      $this->session = Shopware()->Session();
      $sOrderVariables = $this->session['sOrderVariables']->getArrayCopy();
      
      var_dump($sOrderVariables);
}

I’m able to see all the order details but the order number

In your case, I think it is better to go with  Sendmail_FilterVariables  - ‘Shopware_Modules_Order_SendMail_FilterVariables’ => ‘onSaveOrder’

you can also find some inspiration here: Advanced Example how to create 2 custom fields, fill them in order process, display them in backend order list as new columns and make them editable in order detail view · GitHub

Still getting same information but no the order number. I’m using cash on delivery as payment method, don’t know if this has anything to do with my problem

 Shopware_Modules_Order_SendMail_FilterVariables worked for me. With this event you do not need to access the session, because it is directly in the filtered variable. I can check the other cases tomorrow.

No, the payment it is not an issue if it is cash or something else.

As Moritz Naczenski says, also on mine side it is working properly, but the question it is after you click on submit order button and you go to the backend you can find the order there or just you’ve got an error before order confirmation ?..

I tried it like this (in the new plugin system):

scheduleClearCache(ActivateContext::CACHE_LIST_DEFAULT);
    }
    public function deactivate(DeactivateContext $context)
    {
        $context->scheduleClearCache(DeactivateContext::CACHE_LIST_DEFAULT);
    }
    public static function getSubscribedEvents()
    {
        return [
            'Shopware_Modules_Order_SendMail_FilterVariables' => 'onOrdermail',
        ];
    }

    public function onOrdermail(\Enlight_Event_EventArgs $args)
    {
        $variables = $args->getReturn();
        $sOrder = $this->container->get('session')->offsetGet('sOrderVariables');

        die(print_r($variables));
    }
}

Both ways (from session and filterevent) worked for me.

Thank you very much, it is working now! Still can’t see it on the session but it’s on the the filterevent