Jump to content

Courier Tracking Module Assistance


Recommended Posts

Hello,

 

I'm investigating writing a tracking module for Fastway Couriers in SA. They provide full access to their API.

 

I'm new to PrestaShop internals, so I will go learn the basics of module writing from the Dev Guide and other free modules, but I have some questions that I think would help speed up my programming.

 

From the courier company, I only want basic tracking information, not courier costs, etc. So I will do an API query with a label/tracking number and get JSON back. I will then parse the JSON and find the statuses in there.

 

My question is: How do I programmatically/from a module change the status of an order correctly and in such a way that the connected/configured status email is sent to the user?

 

If someone could point me to example code or files in the Core to read, I would greatly appreciate it.

 

Regards,

ProSci

Link to comment
Share on other sites

Hi,

 

Things are going well. I can find the orders that I want to modify and change their statuses. Then the call to addWithEmail() sends the required email. I can also query and parse the data from the courier company.

 

I'm stuck a bit with how to implement this in a way that I can run from a URL. For now I've been using a test module where all this code runs on install/init.

 

I followed a tutorial to create a ModuleAdminController. If I call this controller via URL in the browser with a GET param of runjob=1, it works (executes my helloworld and  creates file). It does not work when using curl. Is there a cookie or something I'm missing?

 

URL example: http://presta.local:8080/admin720shtsiy/index.php?controller=AdminTestModuleTestController&token=92cea04e5b1a2d792a7c59c00736caa0&runjob=1

    public function initContent() {
        parent::initContent();
        if (Tools::getValue('runjob')==1){
            $this->helloworld();
        }
        $this->setTemplate('testcontroller.tpl');
    }

    public function helloworld() {
        $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
        $txt = "John Doe\n";
        fclose($myfile);
    }

Thx.

Link to comment
Share on other sites

I suppose, besides my question in the previous post let me define what it is I'm trying to do.

 

I want to be able to regularly call code that sits in a module, in order to check the tracking

status of an order while it's out on delivery. This code will then update the status of the order.

 

I will need a cron job for this, but first I need some way to call the code sitting in the module

via a URL or something. Unless there is a better way to do all this?

Link to comment
Share on other sites

In my AJAX Dropdown Categories module, I have modules/categoriesnc/cron.php with the following to regenerate the product count cache on a regular basis:
<?php

include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/categoriesnc.php');

if (Tools::getValue('secure_key')) {
    $secure_key = Configuration::get('CATEG_NC_SECURE_KEY');

    if (!empty($secure_key) && $secure_key === Tools::getValue('secure_key')) {
        if (Configuration::get('CATEG_NC_NUM_PRODUCTS') != 2 && Configuration::get('CATEG_NC_HIDE_EMPTY_CATEG') != 1) {
            echo 'Cache regeneration unnecessary';
        } else {
            $categories_nc = new CategoriesNC();
            $categories_nc->createCache();
            echo 'Cache regeneration successful';
        }
    } else {
        echo 'Secure key is incorrect';
    }
} else {
    echo 'Secure key is missing';
}

I use a secure key to prevent the file being called directly. I create a new instance of the module and then call a public function.

Link to comment
Share on other sites

Hi Rocky,

 

Excellent, that is a much simpler solution thank you. Now for the stupid question, how do I call cron.php?

I obviously can't just do so from a command line:  $ php cron.php as it's PrestaShop specific PHP style.

Link to comment
Share on other sites

There is a "Cron Jobs" module you can use to create cron jobs, though I've read reports of it being unreliable. You can log in to your website's cPanel and then click the "Cron Jobs" button in the "Advanced" section. Enter the path to the module's cron.php file along with the secure key and then set how often you want it called.

Link to comment
Share on other sites

Thank you. I'm sorry I harped on about this. What I couldn't figure out, was the actual URL.

 

It appears it is as simple as this:

 

http://presta.local/modules/mymodule/cron.php?secure_key=somkey

 

I totally got lost in all the Prestashop module stuff forgetting that it's basically just accessing a file on a webserver lol.

Edited by prosci8 (see edit history)
Link to comment
Share on other sites

  • 4 years later...
  • 2 months later...
  • 1 year later...

To programmatically change the status of an order and send the corresponding status email to the user in PrestaShop, you can use the OrderHistory class.

Here is an example code snippet that should give you an idea of how to use the OrderHistory class:

php
Copy code
$order = new Order($order_id); // Replace $order_id with the ID of the order you want to update
$order_history = new OrderHistory();
$order_history->id_order = $order_id;
$order_history->changeIdOrderState($new_state_id, $order_id);
$order_history->addWithemail(true, array(
    'order_name' => $order->getUniqReference(),
));
In this code, $new_state_id is the ID of the new status you want to set for the order, and $order_id is the ID of the order you want to update. The addWithemail method sends the status update email to the customer.

To find the ID of the order state you want to set, you can look in the ps_order_state database table. You can also use the st courier tracking class to get the ID programmatically:

php
Copy code
$order_state = new OrderState();
$new_state_id = $order_state->getIdByIso('your_state_iso_code');
Replace 'your_state_iso_code' with the ISO code of the order state you want to set.

As for example files in the core to read, you can take a look at the OrderHistoryController class in controllers/admin/AdminOrdersController.php. This controller handles the order status updates in the PrestaShop back office.

Edited by lilon3 (see edit history)
Link to comment
Share on other sites

  • 1 year later...

Hello ProSci,

For updating the order status programmatically in PrestaShop and ensuring the configured status email is sent, you can use the OrderHistory class. Here's a basic example of how you can achieve this:

Retrieve the Order: Use the order ID to load the order.

Create an OrderHistory Object: This object will hold the new status.

Change the Status: Set the new status on the OrderHistory object.

Save and Send Email: Save the changes and the status update email will be sent automatically.

Here's some example code:

$order = new Order($id_order);

$order_history = new OrderHistory();

$order_history->id_order = $order->id;

$order_history->changeIdOrderState($new_status_id, $order, true);

// true indicates email sending $order_history->add();

Replace $id_order with your order ID and $new_status_id with the new status ID.

For detailed information, refer to the Order and OrderHistory classes in PrestaShop's Core. Also, reviewing some of the existing modules that handle order status updates can be very helpful.

For your xdp tracking integration, follow similar steps for fetching the tracking info and updating the order status accordingly.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...