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:

[php]
if (Tools::isSubmit(‘submitShippingNumber’) && isset($order))
{
..
[/php]

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

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

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:)

[php]

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

[/php]

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

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

$history->changeIdOrderState((int)$shipped_status, $order);
[/php]

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.

[php]
$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’]);
}
[/php]

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

[php]

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’]);
[/php]

Need PrestaShop Modules? Have a look at my Prestashop Addons Store!

Looking for quality PrestaShop Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , | Posted under PrestaShop | RSS 2.0

Author Spotlight

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

Leave a Reply

Your email address will not be published. Required fields are marked *