William K. Posted September 7, 2017 Share Posted September 7, 2017 Hello, I am currently working on some prestashop back office changes on a site already functioning site in order to improve the day to day work of the people using it daily. One particular addition they need is to be able to modify the payment method from the order page. I am struggling with this point because I don't find an equivalent of the postProcess condition "submitState" in adminOrdersController.php like a used to create a changing state button previously. I tried to emulate something similar but I can't find an equivalent of "getCurrentOrderState", can you please help me create one ? Sorry for my broken english and thanks in advance. Link to comment Share on other sites More sharing options...
Scully Posted September 7, 2017 Share Posted September 7, 2017 (edited) There is no built-in way to handle this. Since you haven't mentionned your prestashop version, I can only refer to 1.6.x. The way the payment method is set PaymentModule.php and this code is only executed at the time of order placement. This said, there is not postProcess condition to change the payment method. Depending on the payment before and payment after method, there would be quite much work to do in order to maintain referential integrity of the order data. Once more culprit would be the order confirmation mail. This mail is only sent after the initial verification has been done and the order has been accepted. When you would manage to change the payment method, a new order confirmation is not sent out. Furthermore if you would try to change from bankwire to PayPal, it would get even more complicated. Summary: feasible for an experiencede developer, but not with some sort of quick change. Edited September 7, 2017 by Scully (see edit history) Link to comment Share on other sites More sharing options...
William K. Posted September 8, 2017 Author Share Posted September 8, 2017 Thank you for your quick answer Scully, The prestashop version is 1.6.1. . The point of the button is for the shop manager to be able to switch from a quote request to the payment method used to finalise the order or to specify how the order was paid in shop when they decide to use the "pay in shop" payment module. It will allow him have a better view of his activities in the "order" tab. So I just need to be able to change the array of "ps_orders -> payment" like I would do if I was using phpmyadmin, but from the backoffice, and not actually changing the payment module used. Do you have any hints for me on how to achieve that ? Link to comment Share on other sites More sharing options...
Scully Posted September 8, 2017 Share Posted September 8, 2017 First you needed to make an override for the file AdminOrdersController.php and add a new function like this: public static function changePaymentMethod() { $id_order = Tools::getValue('id_order') if (isset($id_order) and $id_order > 0) { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('update `'._DB_PREFIX_.'orders` set payment = 'mypaymentmethod' WHERE id_order = ' . $id_order ; // add some error handling here .... } } And then you have to enhance the template file for the order overview with a new button. Link to comment Share on other sites More sharing options...
William K. Posted September 8, 2017 Author Share Posted September 8, 2017 (edited) Thank you again but trying to apply your advices, I keep getting an ERROR 500. The button is placed and call the function just right, but the SQL part seems to be causing troubles. (I added a semicolon at the end of "$id_order = Tools::getValue('id_order')" and closed the ExecuteS with " '); " instead of " ; ". ) Edited September 8, 2017 by William K. (see edit history) Link to comment Share on other sites More sharing options...
Scully Posted September 8, 2017 Share Posted September 8, 2017 (edited) Error 500 is a syntax error. Try using a syntax checker and upload the changed file. For example here: http://www.meandeviation.com/tutorials/learnphp/php-syntax-check/v5-3/syntax-check.php Edited September 8, 2017 by Scully (see edit history) Link to comment Share on other sites More sharing options...
William K. Posted September 8, 2017 Author Share Posted September 8, 2017 Yes but like I tried to explain the syntax error seems to be in the Update part of the ExecuteS and I don't find any documentation on it should be used. Link to comment Share on other sites More sharing options...
Scully Posted September 8, 2017 Share Posted September 8, 2017 (edited) Post you code - and here is the documentation for database access: http://doc.prestashop.com/display/PS16/Best+Practices+of+the+Db+Class Edited September 8, 2017 by Scully (see edit history) Link to comment Share on other sites More sharing options...
William K. Posted September 8, 2017 Author Share Posted September 8, 2017 Well it's pretty close to what you sended public static function changePaymentMethod() { $id_order = Tools::getValue('id_order'); // added the missing semicolon if (isset($id_order) and $id_order > 0) { $this->errors[] = Tools::displayError('Je suis dans changePaymentMethod'); // added to be sure that the button trigger the function $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' UPDATE `'_DB_PREFIX_.'orders` SET `payment` = 'TestPayement' WHERE `id_order` = $id_order'); } } Link to comment Share on other sites More sharing options...
Scully Posted September 9, 2017 Share Posted September 9, 2017 Run the syntax checker and you will find the bugs and it helps to learn how php works. And yes - indeed my code also contained bugs. If just wrote it freehand with not checks at all. Link to comment Share on other sites More sharing options...
William K. Posted September 11, 2017 Author Share Posted September 11, 2017 Hi again. I am still working on this, trying to turn the thing in every way possible but my SQL command still don't reach the DB and with no errors i'm not sure how to correct myself. The function looks a lot like this atm : public static function changePaymentMethod() { $id_order = Tools::getValue('id_order'); if (isset($id_order) and $id_order > 0) { Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'orders` SET `payment` = `id_payment` WHERE `id_order` = $id_order'); } } It looks pretty simple (maybe a bit too much), if you have any hints for me it would be very welcome. Link to comment Share on other sites More sharing options...
Scully Posted September 11, 2017 Share Posted September 11, 2017 (edited) your function must be triggered from somewhere. It is not executed on its own. Furthermore if the function is triggered correctly, check wheter id_order is set or not. Insert this code after $id_order is set. This writes a debug message to the prestashop database logfile. PrestaShopLogger::addLog("My debug message - i_order : $id_order ", 1, null); Edited September 11, 2017 by Scully (see edit history) Link to comment Share on other sites More sharing options...
William K. Posted September 11, 2017 Author Share Posted September 11, 2017 I already checked that the function is triggered (with a displayError, pretty sure it realy isn't the best way to check but at least i'm sure i'm going into the function whenever I hit the button) and id_order is set because I can see the displayError even if it is placed after the isset($id_order). Link to comment Share on other sites More sharing options...
Scully Posted September 11, 2017 Share Posted September 11, 2017 (edited) Use getMsgError() to get the status of your database statement. I initially wrote you should add some error handling. Or set the SQL statement into a variable and write it to the prestashop logfile prior to executing the code. This way we could get the SQL in clear text from the logfile an check if it looks correctly. Edited September 11, 2017 by Scully (see edit history) Link to comment Share on other sites More sharing options...
William K. Posted September 11, 2017 Author Share Posted September 11, 2017 The log is working just fine : "My debug message - i_order : 127184" I'm not sure how to use getMsgError(), do you have any documentation about it ? Link to comment Share on other sites More sharing options...
Scully Posted September 11, 2017 Share Posted September 11, 2017 As alreday said: I would also log the entier SQL for the moment. As for getMsgError Search always helps ... https://www.google.ch/search?q=site%3Aprestashop.com+%22getMsgError%28%29%22&ie=utf-8&oe=utf-8 Link to comment Share on other sites More sharing options...
William K. Posted September 11, 2017 Author Share Posted September 11, 2017 I am not sure about what you mean, is it something like this ? $result = 'UPDATE `'._DB_PREFIX_.'orders` SET `payment` = `id_payment` WHERE `id_order` = $id_order'; if (isset($id_order) and $id_order > 0 && isset($id_payment)) { PrestaShopLogger::addLog("$result", 1, null); } The log gives pretty obviously UPDATE `ps_orders` SET `payment` = `id_payment` WHERE `id_order` = $id_order but I'm pretty sure that wasn't what you asked.. Link to comment Share on other sites More sharing options...
Scully Posted September 11, 2017 Share Posted September 11, 2017 you have wrong useage of apostophe. This can't work. WHERE `id_order` = $id_order'); It must be WHERE `id_order` = ' . $id_order); Just PHP basics Link to comment Share on other sites More sharing options...
William K. Posted September 11, 2017 Author Share Posted September 11, 2017 Oh yeah. So my log looks like that after pressing the Credit Card button now : UPDATE `ps_orders` SET `payment` = Credit Card WHERE `id_order` = 127214 Isn't it what the SQL should look like ? Because sadly it still don't change the table. Link to comment Share on other sites More sharing options...
Scully Posted September 11, 2017 Share Posted September 11, 2017 No it isn't good. Where shoud id_payment come from? id_payment assumes it would be a numeric value. But your cleartext sql does show 'credit card' also with wrong use of apostrophes. Link to comment Share on other sites More sharing options...
William K. Posted September 11, 2017 Author Share Posted September 11, 2017 (edited) No it was just stupid from me to call the variable "id_payment" but payment is a varchar in the table and the SQL used had the apostrophes changed like this : 'UPDATE `'._DB_PREFIX_.'orders` SET `payment` = '.id_payment.' ` WHERE `id_order` = ' .$id_order Still, I am doing something wrong ... Edited September 11, 2017 by William K. (see edit history) Link to comment Share on other sites More sharing options...
William K. Posted September 11, 2017 Author Share Posted September 11, 2017 I might still have an apostrophes issue because the value of id_payment appear without apostrophes in the cleartext SQL, and I can't seem to be able to add them ... Link to comment Share on other sites More sharing options...
Scully Posted September 11, 2017 Share Posted September 11, 2017 You have apostophe issues indeed. Values for alphanumeric fields require an apostophe in an sql statement. But this is no a forum for php basics. See here for further information on how to use quotes: php.net/manual/en/language.types.string.php Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now