Jump to content

Changing order status vith php


Draq

Recommended Posts

Hello all,

is there some nice person, which can write me simple example of how to change order status with php script?

I think that in PS is some function for this, but I am new to object programming in php. So would be someone so nice to show me hot to easily change status order with php?

 

Thank you very much

Link to comment
Share on other sites

Hi, try some like this:

 

public function hookOrderConfirmation($params)
{
....
$objOrder = $params['objOrder'];
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState((int)(Configuration::get($this->name.'_status_id')), (int)($objOrder->id));
....
}

 

Regards

Edited by Alexander Simonchik (see edit history)
  • Like 4
Link to comment
Share on other sites

I am sory for disturbing you, but when I have code:

<?php
require_once '../classes/order/Order.php';
require_once '../classes/order/OrderHistory.php';

$objOrder = new Order(1); //order with id=1
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState(3, (int)($objOrder->id)); //order status=3
echo 'done'; 
?>

 

It wortes me fatal error: Class 'ObjectModel' not found in C:\wamp\www\classes\order\Order.php on line 29

 

I have PS 1.5.0.17, Thank you so much!

Link to comment
Share on other sites

  • 5 months later...

Hello, I'm working on a Prestashop for a friend. This code works like a charm for me.

 

It sets the Order-Status perfectly!

 

I only got one Problem, my friend wants the automated eMail which is send if you switch the status in the Backend to be send via my Script too... so if I for example set the order status to 4, it should send out an eMail that the article was given to delivery!

 

Can you help me somehow?

 

Right now my code looks like this:

require('../config/config.inc.php');
require_once '../classes/order/Order.php';
require_once '../classes/order/OrderHistory.php';

$objOrder = new Order($_GET["order"]); //order with id=$_GET["action"]
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState($_GET["status"], (int)($objOrder->id)); //order status=4
$statusmeldung = '<h1 style="background: rgb(255,0,0); margin-top: 214px;">ORDER ID '.$_GET["order"].' auf Status '.$_GET["status"].' gesetzt!</h1>';

 

Thanks in advance

-Christian

Edited by expmedia-Christian (see edit history)
Link to comment
Share on other sites

that's right Alex has got absolutely right, when you use default classes and script to change order status - then email will be automatically send, just turn it on under the page that Alex suggested

 

i've noticed (from your code) that you use default classes (in this case order history & order classes) so it will work for you

(hope you use ps 1.5)

  • Like 1
Link to comment
Share on other sites

Hi, thanks for your help!

 

Yes we use Prestashop 1.5.4.1.

 

The eMail in the status is set, here are some screenshots.

emailtemplate.png

 

Here's how the order looks if I set it to status 4 via my external script:

howitlooks.png

 

There's this missing:

missing.png

 

But the order itself is on Status 4 :(... I guess that's why there's no eMail going out?

Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...

Hello, I'm working on a Prestashop for a friend. This code works like a charm for me.

 

It sets the Order-Status perfectly!

 

I only got one Problem, my friend wants the automated eMail which is send if you switch the status in the Backend to be send via my Script too... so if I for example set the order status to 4, it should send out an eMail that the article was given to delivery!

 

Can you help me somehow?

 

Right now my code looks like this:

require('../config/config.inc.php');
require_once '../classes/order/Order.php';
require_once '../classes/order/OrderHistory.php';

$objOrder = new Order($_GET["order"]); //order with id=$_GET["action"]
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState($_GET["status"], (int)($objOrder->id)); //order status=4
$statusmeldung = '<h1 style="background: rgb(255,0,0); margin-top: 214px;">ORDER ID '.$_GET["order"].' auf Status '.$_GET["status"].' gesetzt!</h1>';
Thanks in advance

-Christian

 

hi

 

am trying to make some trick, i want to change the order status with the Order and OrderHistory classes from the PS at another php module different to the PS, am using the 1.5.4.1 Version already, the issues is than when am going to require require('../config/config.inc.php'); for those lines  $context->shop = Shop::initialize();

 
define('_THEME_NAME_', $context->shop->getTheme());
define('__PS_BASE_URI__', $context->shop->getBaseURI()); i can't do nothing, thats redirecting me to the official frontend WS where's been the Prestashop site
 
please if some budy can provide some fix i realy appreciate it
Link to comment
Share on other sites

  • 6 months later...
  • 2 months later...

And that's the issue, it should work with the external script too, is there any possiblity to get this working?

I know it is an old thread but I'm in the same situation, the order status has been changed, but the emails were not sent.

 

I found solution to the Alex original code:

$objOrder = new Order(1); //order with id=1
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState(3, (int)($objOrder->id)); //order status=3

just add this at the end:

$history->addWithemail(true);

in my PS version 1.4.10 it works perfectly :)

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...
Hello, 
I can not run the script on my site, I have prestashop 1.5.6.2, I tried to update an order but the script is as if they were empty, I forget something?
 
<?php
echo "<center><h1>Aggiorna Ordine</h1></center>";
require('../web2014/config/config.inc.php');
require('../web2014/classes/order/Order.php');
require('../web2014/classes/order/OrderHistory.php');


$objOrder = new Order(14203883); //order with id=1

$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState(4, (int)($objOrder->id)); //order status=3



?>

 

Link to comment
Share on other sites

Ciao,

questo è l'errore :

 

Fatal error: Cannot redeclare class OrderCore in /var/www/vhosts/mysite.it/httpdocs/web2014/classes/order/Order.php on line 1967

 

 

Ho aperto Order.php, ma il rigo 1967, corrisponde a fondo pagina, non riesco ad individuare la problematica.

Link to comment
Share on other sites

Here is the error that I get

 

Fatal error : Cannot redeclare classe OrderCore in  / var/www/vhosts/mysite.it/httpdocs/web2014/classes/order/Order.php on line  1967

 

I opened order.php, but the line 1967 corresponds to the bottom of the page, I can not identify the problem.

 

Please help me

Link to comment
Share on other sites

  • 3 weeks later...

I have the exact same Problem.

 

Either I try:

$history = new OrderHistory();
$history->id_order = (int)$id;
$history->changeIdOrderState((int)$this->ID_ORDER_STATE_EXPORTED, $history->id_order);
$history->addWithemail();
$history->save();

or

$order = new Order($id);
$order->setCurrentState($this->ID_ORDER_STATE_EXPORTED);

I get the error:

 

Cannot redeclare class OrderCore in [...]/classes/order/Order.php on line 2068

 

The error is triggered by $order->setCurrentState and by $history->changeIdOrderState. The object/instances are created without any problems. Only calling the functions/methods does not work. What am I doing wrong?

 

Thanks

 

 
 

 

Link to comment
Share on other sites

errors like that appear usually when library was attached before somewhere else

usually it's related to modules that attach order class

do you use some not default  payment modules?

 

Thanks, vekia. Actually I'm developing a module to export orders as an xml file and change the state accordingly. I don't include or attach or redeclare the class in my module as I assume PrestaShop includes its core classes as needed. I instantiate Order objects without any problems, only when I call the $xxxx->setCurrentState() function it fails with mentioned error message. Do I miss some basic concept of PrestaShop development? Can't I use core classes for module development like using an API? 

 

UPDATE:

 

I solved it. I had a line where I asked for an array of order states with a given state like:

 

$orderIDs = OrderCore::getOrderIdsByStatus($this->ID_ORDER_STATE_PAYED);

 

I changed it to:

 

$orderIDs = Order::getOrderIdsByStatus($this->ID_ORDER_STATE_PAYED);

 

and now it works. I really missed some kind of basic concept here. I still would appreciate some advise on when using SomeclassCore and when to use just Someclass. Any comments?

 

Thanx anyways.

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

  • 2 weeks later...

Hello, I have a Prestashop 1.6.0.6 version. I try this code but the order status did not change. I did not receive any error.

 

Someone know how do it on Prestashop 1.6 version? Thanks.

Which code did you try? 

 

If you want to change a state of the order programatically, you need to create an order object with the id of the order and then set the current state with the order state id. 

 

These two lines should do the trick.

$order = new Order($id_order);
$order->setCurrentState($id_order_state);
Link to comment
Share on other sites

Hello, I used the following code and don't work.

require_once('../config/config.inc.php');
require_once '../classes/order/Order.php';
require_once '../classes/order/OrderHistory.php';
               
$objOrder = new Order($id_order);
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState(3, (int)($objOrder->id));

I tryed as you suggest but nothing change.

require_once('../config/config.inc.php');
require_once '../classes/order/Order.php';
require_once '../classes/order/OrderHistory.php';
              
$order = new Order($id_order);
$order->setCurrentState(3);   
Edited by ItalianBoutiqueShoes (see edit history)
Link to comment
Share on other sites

Did you actually try to replace the $id_order variable with an existing ID of an order? You shouldn't need to include the classes as the line

 

require_once(dirname(__FILE__).'/../../config/config.inc.php');

 

does the job. In modules context I hat to include it with ../../ to get it working. What exactly are you trying to do?

 

Regards,

Link to comment
Share on other sites

My interelly code is the follow

$id_order = 3;

require_once('../config/config.inc.php');
require_once '../classes/order/Order.php';
require_once '../classes/order/OrderHistory.php';
                
$order = new Order($id_order);
$order->setCurrentState(3);

So, yes the $id_order variable is set.

 

 

This code is on a file on the admin folder. If I change the path I receive the following error so the classes are well included

Warning: require_once(/config/config.inc.php) [function.require-once]: failed to open stream: No such file or directory in xxx on line 13

Fatal error: require_once() [function.require]: Failed opening required '/config/config.inc.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in xxx on line 13
Link to comment
Share on other sites

I found a solution: it work like this: you need to require only config.inc.pho file.

I hope it send also email confirmation. It is for version 1.6 and 1.5 I think.

$id_order = 3;   
require_once('../config/config.inc.php');
//require_once '../classes/order/Order.php';
//require_once '../classes/order/OrderHistory.php';
              
$order = new Order($id_order);
$order->setCurrentState(3);  
Edited by ItalianBoutiqueShoes (see edit history)
  • Like 3
Link to comment
Share on other sites

 

I found a solution: it work like this: you need to require only config.inc.pho file.

I hope it send also email confirmation. It is for version 1.6 and 1.5 I think.

$id_order = 3;   
require_once('../config/config.inc.php');
//require_once '../classes/order/Order.php';
//require_once '../classes/order/OrderHistory.php';
              
$order = new Order($id_order);
$order->setCurrentState(3);  

 

This one is the only solution works for me.

 

The previous solution using changeIdOrderState function didn't work for me. The function only change $order->current_state and not changing $order->id_order_state.

Link to comment
Share on other sites

Hi Community !!

I try the same, but the order state doesn't change. I'm using 1.4.7. Could this be the problem?

I know the Classes are directly in directory "classes" not "classes/order".

 

Do you know a working solution for 1.4.7 ?

 

Thanks for your help!

 

thats my code:

public function hookOrderConfirmation($status,$orderid)
    {
        require('../config/config.inc.php');
        require_once '../classes/Order.php';
        require_once '../classes/OrderHistory.php';

        $objOrder = new Order($orderid);
        $history = new OrderHistory();
        $history->id_order = (int)$objOrder->id;
        $history->changeIdOrderState($status, (int)($objOrder->id)); 
     }
Edited by pneumon1c (see edit history)
Link to comment
Share on other sites

  • 1 month later...

Hi,
I'm quite new to prestashop too...
I'm running this on my local host. When it runs, it loads my homepage and do nothing.

Any clue?
 
 
I use this code on prestashop 1.6.0.8:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
    <head>
        <title>Page de gestion des envois : echo</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
        <?php
        require(dirname(__FILE__).'/../config/config.inc.php');
        //require('../prestashop/config/config.inc.php');
        require_once '../classes/order/Order.php';
        require_once '../classes/order/OrderHistory.php';



        $objOrder = new Order(1); //order with id=1
        $history = new OrderHistory();
        $history->id_order = (int)$objOrder->id;
        $history->changeIdOrderState(3, (int)($objOrder->id)); //order status=3
        echo 'done';
        ?>
            </body>
</html>

Link to comment
Share on other sites

Actually, I'm wondering if I can simply change it in the database that way: Will it mess with prestashop or is it doing the job?

 

 $db-> exec("UPDATE ps_orders
            SET current_state = 4
            WHERE id_order = $id_comm");
            date_default_timezone_set('America/Montreal');
            $date_stat=date("Y-m-d H:i:s");
           
        $upt_st= $db->prepare('INSERT INTO ps_order_history(id_order, id_order_state, date_add) VALUES(?, ?, ?)');
        $upt_st->execute(array($id_comm, 4, $date_stat));

Link to comment
Share on other sites

I recommend to use:

$order = new Order($id_order);
$order->setCurrentState($id_order_state);  

rather than using OrderHistory class directly which also works, but does not send emails even if the new status is configured in the back office to trigger emails :

$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState($status, (int)($objOrder->id)); 

If you look inside the code for Order::setCurrentState() method  (in Prestashop 1.5) you can see that it executes almost the same above code with OrderHistory class plus some more code.

This additional code does an update action on the order itself, and executes OrderHistory::addWithemail() method, which also ensures the email notification will be sent for status change.

 

Link to comment
Share on other sites

Actually, I'm wondering if I can simply change it in the database that way: Will it mess with prestashop or is it doing the job?

 

 $db-> exec("UPDATE ps_orders

            SET current_state = 4

            WHERE id_order = $id_comm");

            date_default_timezone_set('America/Montreal');

            $date_stat=date("Y-m-d H:i:s");

           

        $upt_st= $db->prepare('INSERT INTO ps_order_history(id_order, id_order_state, date_add) VALUES(?, ?, ?)');

        $upt_st->execute(array($id_comm, 4, $date_stat));

 

This is definitely not a good idea in my opinion. Do this only if you really know what you are doing. Because, you will be breaking/bypassing the whole hook systems available in Prestashop by doing it like this. That means you will loose a lot of nice features available to configure in backoffice. Email notifications for status change is just one example.

You will definitely end up writing lot more customization code if you take a path like this.

Don't reinvent the wheels. If you want to get the maximum benefit of using prestashop, use the core classes as much as possible.

  • Like 1
Link to comment
Share on other sites

Thanks for your opinion. Do you know what's wrong with these line:

 

require(dirname(__FILE__).'/../config/config.inc.php');

 

        require_once '../classes/order/Order.php';

        require_once '../classes/order/OrderHistory.php';

 

I can't say anything for sure because you have given relative paths.

The correctness of these lines depends on where you have written this code. Where have you placed the file with these lines?

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

Working now!

For information:

The relative path was set correctly but I  change with the complete path of the file from C: and it was still loading the homepage.

 

I then move my code in the same folder than config.php and it was not loading the homepage anymore.

 

Finally I kept: require('C:\xampp\htdocs/prestashop/config/config.inc.php');

and removed the require to Order.php and OrderHistory.php.

and it worked.

 

Thanks for your help everybody!

Link to comment
Share on other sites

  • 2 weeks later...

I know it is an old thread but I'm in the same situation, the order status has been changed, but the emails were not sent.

 

I found solution to the Alex original code:

$objOrder = new Order(1); //order with id=1
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState(3, (int)($objOrder->id)); //order status=3

just add this at the end:

$history->addWithemail(true);

in my PS version 1.4.10 it works perfectly :)

The code works fine and even sends the notification email. But the generated PDF attached to the email has some warnings like those shown below. Can someone tell me how should I get rid of these? Thank you in advance!

 

Warning: include(/presta/translations/en/pdf.php): failed to open stream: No such file or directory in /presta/classes/Translate.php on line 214

 
Warning: include(): Failed opening '/presta/translations/en/pdf.php' for inclusion (include_path='/presta/tools/htmlpurifier/standalone:.:/usr/lib/php:/usr/local/lib/php') in /presta/classes/Translate.php on line 214
Edited by clawzee (see edit history)
Link to comment
Share on other sites

  • 10 months later...

 

I confirm that the following code will also send emails when setted properly.

require_once('../config/config.inc.php');
              
$order = new Order($id_order);
$order->setCurrentState(3);  

Hi everybody !

 

I tried a bunch of propositions here and nothing helps : as soon as I put 

require('../config/config.inc.php');

in my script, it stops here, doing nothing with no error and no warning..

My script is on the admin directory..

PS version 1.6.1

 

Any idea ?

Link to comment
Share on other sites

Hello everyone

 

I know it is a bit old thread but seemed to be close to my problem.

 

I'm running php script to read xml file. From that xml file i'm getting order id and tracking code for the order.

The script updates the tracking code into database and changes the orderstatus afterwards.

 

Now the problem is that when the script changes the order status, it does send the shipping email to customer but the shipping email is not working correctly.

The shipping email is missing the "{followup}"(trackingcode/url) information if i change the status by my script.

But if i run the script without changing the status, and change the status manually from BO the shipping email now has the working {followup} information from database.

 

Any ideas why this is happening if i change the orderstatus from script instead of BO?

P.s Tried changing the status with setCurrentState and changeIdOrderState, with no difference in end result.

Link to comment
Share on other sites

  • 2 weeks later...

@yaso,

 

every time you change something on your carrier, the system makes a new carrier with new ID.

I think also that this is not a good way prestashop handles changes to carriers.

if you look at your db and table carriers, you will see you will have one extra after each change ;-)

 

so make a new test order, and test your script, it will work ;-)

 

D.

Link to comment
Share on other sites

  • 1 month later...
  • 6 months later...

When I tried to change order status as customer are not getting emails when order status is changed

 

I got this error

 

Hi I just updated my shop to PS 1.6.1.5 and tried to change the order status like to go Order--> status and tried to mark few fields I got this error

 

 

An error occurred while updating an object. order_state (Unknown column 'pdf_delivery' in 'field list')

 

Not sure what is the issue

 

Installing old version of mail alerts can help me?

 

Please respond asap

Link to comment
Share on other sites

When I tried to change order status as customer are not getting emails when order status is changed

 

I got this error

 

Hi I just updated my shop to PS 1.6.1.5 and tried to change the order status like to go Order--> status and tried to mark few fields I got this error

 

 

An error occurred while updating an object. order_state (Unknown column 'pdf_delivery' in 'field list')

 

Not sure what is the issue

 

Installing old version of mail alerts can help me?

 

Please respond asap

 

pdf_delivery is field in the ps_order_state table. Check this table. You should see 14 fields.

Link to comment
Share on other sites

  • 4 months later...
  • 4 months later...

For prestashop 1.6.1.7
 
When I check into the code, it need additional code :
 
This is works for me
 
If you want save without send email

 
$objOrder = new Order(1); //order with id=1
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState(3, (int)($objOrder->id)); //order status=3
$history->add(true);

If you want save with send email

 
$objOrder = new Order(1); //order with id=1
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState(3, (int)($objOrder->id)); //order status=3
$history->addWithemail(true);
  • Like 3
Link to comment
Share on other sites

Hi, I have a script which is changing order status from external program^ but it is changing order status directly in the database. Can somebody help me to change the script in order to use native Prestashop status change?

 

Here is my code:

<?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
include(dirname(__FILE__).'/config/config.inc.php');





$result = Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'orders where date_add >= now() - INTERVAL 24 HOUR;');

if(isset($result) && !empty($result)){
    foreach($result as $item){
        if((int)$item['current_state']==5){continue;}
        $url=" XXXXXXXXX".
            "idOrder=".$item['shipping_number'].
            "&user=test".
            "&password=123456".
            "&cid=4541";

        $mych = curl_init();
        curl_setopt($mych, CURLOPT_URL, $url);
        curl_setopt($mych, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($mych, CURLOPT_HEADER, 0);
        $data = curl_exec($mych);
        curl_close($mych);
       /* echo "<pre>";
        print_r($data);
        echo "</pre>";*/
        $rez="";
        if(isset($data) && !empty($data) && $data != 'null' ){
            $rez = json_decode($data, true);


            switch ((int)$rez['status']){
            
                case 3:
                    Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'orders SET current_state = 1 WHERE shipping_number = '.$item["shipping_number"]);
                    break;
                case 4:
                    Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'orders SET current_state = 1 WHERE shipping_number = '.$item["shipping_number"]);
                    break;
                case 5:
                    Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'orders SET current_state = 1 WHERE shipping_number = '.$item["shipping_number"]);
                    break;
                case 6:
                    Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'orders SET current_state = 1 WHERE shipping_number = '.$item["shipping_number"]);
                    break;
                case 7:
                    Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'orders SET current_state = 1 WHERE shipping_number = '.$item["shipping_number"]);
                    break;
                case 8:
                    Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'orders SET current_state = 4 WHERE shipping_number = '.$item["shipping_number"]);
                    break;
                
                default:
                    Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'orders SET current_state = 3 WHERE shipping_number = '.$item["shipping_number"]);

            }

        }else{
            /*Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'orders` SET `current_state` = 5 WHERE `shipping_number` = '.$item["shipping_number"]);*/

        }

    }
}

echo 1;
?>

Regards

Suren

Link to comment
Share on other sites

  • 4 months later...
  • 1 month later...

Bonjour,

 

Ayant été confronté à cette problématique, voilà la solution qui a fonctionné (Presta v1.6.1.11) :

$new_history = new OrderHistory();
$new_history->id_order = (int) $id_order;
$new_history->id_order_state = (int) $mon_statut;
$new_history->add(true);
$new_history->save();
  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Hey there ! 

This topic is quite old, but i see we still have the same requirements ! 

 

I would like to change the status of EVERY orders with status X, and apply them status Y. 

 

The code in this topic actually just change the status for order id = Z  (one order only..) 

 

Hope you can help ! 

cheers

Link to comment
Share on other sites

  • 2 months later...

Hello.

Here is how you can change the state of a group of orders from a cron task:

 

DEFINE('TOKEN','123456');

if(!(isset($_REQUEST['token']) && $_REQUEST['token'] == TOKEN))
    die('Access forbidden');

require_once('../config/config.inc.php');

DEFINE('ENVIO_CREADO_TOURLINE_EXPRESS', 30);
DEFINE('ENVIADO', 4);
DEFINE('PAGO_ACEPTADO', 2);
DEFINE('PREPARACION_EN_PROCESO',3);
DEFINE('ENTREGADO',5);
DEFINE('CANCELADO', 6);

// From /classes/order/Order.php
// Marked as deprecated
// getOrdersIdByDate($date_from, $date_to, $id_customer = null, $type = null)
// getOrderIdsByStatus($id_order_state)

foreach(Order::getOrderIdsByStatus(ENVIO_CREADO_TOURLINE_EXPRESS) as $order)
{
    //echo 'Order n.: ' . $order . "\n";
    
    $objOrder = new Order($order); 
    $history = new OrderHistory();
    $history->id_order = (int)$objOrder->id;
    $history->changeIdOrderState(ENVIADO, (int)($objOrder->id)); 
    //$history->addWithemail(true);
    $history->add(true);
}

echo 'Updated.';
 

I define the different states at the top. Later I search for the orders with the state I want to change and with a foreach I change the state to those orders.

Please, note that getOrderIdsByStatus and getordersIdByDate (if you want to search by dates) are deprecated, but by now they work. I've tested with Presta 1.6.1.9

Write that code in a php file and add it to a cron task, for example http://mydomain.com/myscripts/change-order-state.php?token=123456

Remember to change token!

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

  • 9 months later...
On 21/10/2014 at 10:09 PM, limonaderose said:

Hi,
I'm quite new to prestashop too...
I'm running this on my local host. When it runs, it loads my homepage and do nothing.

Any clue?
 
 
I use this code on prestashop 1.6.0.8:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
    <head>
        <title>Page de gestion des envois : echo</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
        <?php
        require(dirname(__FILE__).'/../config/config.inc.php');
        //require('../prestashop/config/config.inc.php');
        require_once '../classes/order/Order.php';
        require_once '../classes/order/OrderHistory.php';



        $objOrder = new Order(1); //order with id=1
        $history = new OrderHistory();
        $history->id_order = (int)$objOrder->id;
        $history->changeIdOrderState(3, (int)($objOrder->id)); //order status=3
        echo 'done';
        ?>
            </body>
</html>

 

Hello.

Can anyone tell me, please, if this code can be used on PS 1.7.3.0 and if not what could be the changes.

I confess my ignorance in this matter, but I'm looking for the code that can change the satus and send emails.

Thank you.

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

  • 6 months later...
On 9/17/2018 at 6:43 PM, el_boda said:

 

Hello.

Can anyone tell me, please, if this code can be used on PS 1.7.3.0 and if not what could be the changes.

I confess my ignorance in this matter, but I'm looking for the code that can change the satus and send emails.

Thank you.

Sorry for late, it worked on 1.7+ ;)

Link to comment
Share on other sites

  • 1 year later...

Hello All,

I am on presta 1.6

I am changing order state on a script , it works simply calling

                        $order->setCurrentState(4,1);  // I have added employee id as well
                        $order->save();
 

Unfortunately it does not trigger hooks on order status update.

Could you please suggest what to do to have triggered as well those hooks ? 

I have a trigger for example to send a SMS when order it's being sent.

 

Thank you,

DAniel

Link to comment
Share on other sites

  • 4 months later...

Dear All,  presta 1.7.6.5

I would like to change order status from external file located:

{Presta_Folder}/some_folder/someFile.php

/*File source*/

<?php

require('../config/config.inc.php');

$objOrder = new Order(1);

//order with id=1

$history = new OrderHistory();

$history->id_order = (int)$objOrder->id;

$history->changeIdOrderState(3, (int)($objOrder->id)); //order status=3

$history->addWithemail();

 

in general, the status change operation work, i mean 

Status has new value,  but no e-mail have been sent - instead of this server responds err:500

it seems there is problem to acces to Mail.php, but maybe i am wrong.

Have you any idea ?

Serwer Logs:

 

[Wed Dec 30 20:00:49 2020] [error] [client xxxx] request="GET / HTTP/1.1" pid=140466905475053 uid=3409069 gid=3409069

[Wed Dec 30 20:22:11 2020] [error] [client xxxxx] request="GET /Some_Folder/someFile.php?id_order=11 HTTP/1.1" pid=140466905431992 uid=3409069 gid=3409069

[Wed Dec 30 20:22:11 2020] [error] [client xxxx] PHP Fatal error: Uncaught Exception: Kernel Container is not available in

{Presta_Folder} /src/Adapter/ContainerFinder.php:77\nStack trace:\n#0

{Presta_Folder} /classes/Tools.php(796): PrestaShop\\PrestaShop\\Adapter\\ContainerFinder->getContainer()\n#1

{Presta_Folder} /classes/Tools.php(773): ToolsCore::getContextLocale(Object(Context))\n#2

{Presta_Folder} /classes/order/OrderHistory.php(513): ToolsCore::displayPrice(85.6, Object(Currency), false)\n#3

{Presta_Folder} /classes/order/OrderHistory.php(468): OrderHistoryCore->sendEmail(Object(Order), false)\n#4

{Presta_Folder} /Some_Folder/ExternalChangeOrderId.php(16): OrderHistoryCore->addWithemail(true)\n#5 {main}\n thrown in

{Presta_Folder} /src/Adapter/ContainerFinder.php on line 77

 

 

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

  • 1 year later...

Hello,

 

The solution provided above does work correctly, thank you very much!

However, i hope someone can point me in the right direction with an error that has come up.

 

If i change an order the order status 5 (delivered) it calls upon a module we have that sends a discount code to the customer during the process of updating order status.

 

It throws the following error:

 

(1/1) ClassNotFoundException

Attempted to load class "LinevenHcomDiscount" from the global namespace.
Did you forget a "use" statement?

in Mail.php line 197

at LinevenHcomMail::sendMailsToCustomer(25138)in BackofficeOrderStatus.php line 28

at LinevenHcomAdminBackofficeOrderStatusController->updateAction()in Controller.php line 114

at LinevenHcomController->run('update')in homecomments.php line 307

at HomeComments->hookUpdateOrderStatus(array('newOrderStatus' => object(OrderState), 'id_order' => 25138, 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 2))in jprestaspeedpack.php line 2601

at Jprestaspeedpack::execHook('m', object(HomeComments), 'hookupdateOrderStatus', array('newOrderStatus' => object(OrderState), 'id_order' => 25138, 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 2))in Hook.php line 31

at Hook::coreCallHook(object(HomeComments), 'hookupdateOrderStatus', array('newOrderStatus' => object(OrderState), 'id_order' => 25138, 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 2))in Hook.php line 359

at HookCore::callHookOn(object(HomeComments), 'actionOrderStatusUpdate', array('newOrderStatus' => object(OrderState), 'id_order' => 25138, 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 2))in Hook.php line 907

at HookCore::exec('actionOrderStatusUpdate', array('newOrderStatus' => object(OrderState), 'id_order' => 25138, 'cookie' => object(Cookie), 'cart' => object(Cart), 'altern' => 2), null, false, true, false, '1')in OrderHistory.php line 107

at OrderHistoryCore->changeIdOrderState(5, 25138)in test.php line 165

 

I hope someone can help, i think i just need to include the module possibly but im not sure.

 

Thanks in advance!

Link to comment
Share on other sites

  • 8 months later...

I was able to make this code work, I got a couple of issues.

I want to replicate the same functionality when an order is changed to Payment Accepted in the BO, which does changes to a few tables.
When order status is changed to Payment Accepted in the BO, a new row is inserted to (at least) both ps_order_history and ps_order_invoice and the respective ps_orders entry is of course updated as well.

While experimenting with this code, I noticed a strange thing:

  • If include the $history->save(); - the row is created into ps_order_history , but not in ps_order_invoice
  • If I comment the $history->save(); - the row is created into ps_order_invoice, but not in ps_order_history

This seems super weird? Any ideas why this is happening? IMO both should be updated?

Also, are there any other tables that should be updated explicitly than the ones mentioned above?

 

I am also getting this error lot fo times:


Notice: Trying to get property 'precision' of non-object in /home/816694.cloudwaysapps.com/xxxx/public_html/classes/Context.php on line 498

Fatal error: Uncaught TypeError: Argument 1 passed to PrestaShop\PrestaShop\Core\Localization\CLDR\ComputingPrecision::getPrecision() must be of the type int, null given, called in /home/816694.cloudwaysapps.com/xxxx/public_html/classes/Context.php on line 498 and defined in /home/816694.cloudwaysapps.com/xxxx/public_html/src/Core/Localization/CLDR/ComputingPrecision.php:41 Stack trace: #0 /home/816694.cloudwaysapps.com/xxxx/public_html/classes/Context.php(498): PrestaShop\PrestaShop\Core\Localization\CLDR\ComputingPrecision->getPrecision() #1 /home/816694.cloudwaysapps.com/xxxx/public_html/classes/order/Order.php(1465): ContextCore->getComputingPrecision() #2 /home/816694.cloudwaysapps.com/xxxx/public_html/classes/order/Order.php(1353): OrderCore->setInvoiceDetails() #3 /home/816694.cloudwaysapps.com/xxxx/public_html/classes/order/OrderHistory.php(371): OrderCore->setInvoice() #4 /home/816694.cloudwaysapps.com/xxxx/public_html/yyyyyyyyy.php(13): OrderHistoryCore->changeIdOrderS in /home/816694.cloudwaysapps.com/xxxx/public_html/src/Core/Localization/CLDR/ComputingPrecision.php on line 41

 



Here's what I'm using (with 1.7.8.7):

<?php
require(dirname(__FILE__).'/config/config.inc.php');

echo 'Includes done'.'<br/>';
echo 'Order ID:'.$_GET["oid"].'<br/>';
echo 'Order Status:'.$_GET["status"].'<br/>';

$objOrder = new Order($_GET["oid"]);

$history = new OrderHistory();

$history->id_order = (int)$objOrder->id;
$history->changeIdOrderState($_GET["status"], (int)($objOrder->id)); //order status 2 = Payment Accepted

//$history->addWithemail(true);
$history->save();
?>

 

 

Edited by GoPure Kratom (see edit history)
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...