Jump to content

Adding a button to change ps_orders payement from BackOffice.


William K.

Recommended Posts

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

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 by Scully (see edit history)
Link to comment
Share on other sites

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

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

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 by William K. (see edit history)
Link to comment
Share on other sites

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

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

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 by Scully (see edit history)
Link to comment
Share on other sites

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

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 by Scully (see edit history)
Link to comment
Share on other sites

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

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 by William K. (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...