Set the order status to Shipped when adding a tracking code in Prestashop

This little trick will save you the time of having to manually change the order status when you add a tracking number for orders, in the Prestashop Back Office.

Before proceeding, be aware that this method is not ideal in case you are offering multi shipping (that is, if the same order is associated to multiple carriers and tracking numbers), as it would be set to shipped even if not all of the items have been.

That said, open up AdminOrdersController.php located in the controllers/admin folder. Locate the following:

if (Tools::isSubmit('submitShippingNumber') && isset($order))
		{
			..

Inside this conditional, we are going to plug in our code, and the ideal spot for it would be right before

// Send mail to customer
	$customer = new Customer((int)$order->id_customer);
	$carrier = new Carrier((int)$order->id_carrier, $order->id_lang);

As at this point the tracking number has already been set. Therefore, let’s start by adding some lines of code (my line is around 465, in Prestashop 1.6.0.14:)


$shipped_status = Configuration::get('PS_OS_SHIPPING');
if($order->current_state != $shipped_status && $order->current_state != Configuration::get('PS_OS_CANCELED'))
{
	// todo
}

Explanation: First, we are grabbing the shipped status from the database configuration, then we check that the current order has not been shipped already (it would be redundant to add another identical status!). We also make sure it’s not canceled, to avoid any conflict if we accidentally (or necessarily) update the tracking after canceling an order

Inside our new condition, add

	$history = new OrderHistory();
	$history->id_order = $order->id;
	$history->id_employee = (int)$this->context->employee->id;

	$history->changeIdOrderState((int)$shipped_status, $order);
 

So that we effectively modify the status. Now that the order is changed, we can do two things: sending an extra email to the customer (For the “shipped” status) and updating stock in case we use advanced stock management.

	$carrier = new Carrier($order->id_carrier, $order->id_lang);
	$templateVars = array();
	if ($order->shipping_number)
		$templateVars = array('{followup}' => str_replace('@', $order->shipping_number, $carrier->url));

	if ($history->addWithemail(true, $templateVars))
	{
		if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
			foreach ($order->getProducts() as $product)
				if (StockAvailable::dependsOnStock($product['product_id']))
					StockAvailable::synchronize($product['product_id'], (int)$product['id_shop']);
	}	
 

Bear in mind that, if you don’t want to send out any email, you migh as well just paste in the last part:


 	if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT'))
			foreach ($order->getProducts() as $product)
				if (StockAvailable::dependsOnStock($product['product_id']))
					StockAvailable::synchronize($product['product_id'], (int)$product['id_shop']);
 

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • Depri Mantias Suryana

    is there a way to trigger order status changed to shipped by not clicking “update button” from backoffice? i’m using StoreCommander and usually do tracking number update from the table, i tried the code and it’s work well from backoffice but not from SC, the code is not triggered

  • Ivan Ivanov

    for Prestashop 1.6.X must add $history->save(); to save record in history table ;)

    • Depri Mantias Suryana

      i applied your suggestion, add $history->save();
      after $history->changeIdOrderState((int)$shipped_status, $order);
      and it’s work, thanks :)

  • Sandra Lili Yin

    Hi Nemo, how to change to ‘Processing in progress’ instead of ‘Shipped’?

  • scott

    Thank you very much for this tip ! It is very very useful !! Or I have a question, is it possible to change the OrderStatus to a Status ID Number that we choose instead of $shipped_status ? Thanks very much for your help

    • NemoPS

      Hello,
      Yes, that would be possible, bu you have to hardcode the ID to that shipped status variable

  • yaniv Jacobson

    Great post! thank you, worked like a charm! nice job.

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: