Changing Order Status According To Payment Method

I want to change order status according to the payment method. If the customer chooses the payment method that ID is 3, the order status has to change and be saved with new changed order status…



I preparted the code and put it under /app/addons/my_changes/controllers/frontend in checkout.post.php file.



But I couldn’t add the right code to necessary area. I need HELP.






<br />
use Tygh\Registry;<br />
if( !defined('AREA') ) {die('Access denied'); }<br />
$cart = & $_SESSION['cart'];<br />
if (isset($cart['payment_id'])) {<br />
$payment_method_data = fn_get_payment_method_data($cart['payment_id']);<br />
}<br />
$order_info = fn_get_order_info($_REQUEST['order_id']);<br />
<br />
if( $_SERVER['REQUEST_METHOD'] != 'POST' && $mode == 'complete' ) {  //I am not sure what mode I should use. complate or checkout?<br />
if ($payment_method_data['payment_id']==3) {<br />
  <br />
<br />
/*** SOME CODES HERE***/<br />
<br />
<br />
  }<br />
<br />
<br />
//fn_print_die($order_info);<br />
<br />
<br />
}<br />
return array(CONTROLLER_STATUS_OK);<br />

Use the change_order_status hook instead (lot easier). It will pass you the status being changed to, possibly the status being changed from, the order_id and even the list of current statuses.



You'd look at the status being changed to. if 'P', then look at the payment method and adjust it to whatever you want.

I cretated an addon instead of checkout.post.php and put a function in func.php



function fn_myaddon_place_order($order_id, &$cart)
{
$cart = & $_SESSION['cart'];
if (isset($cart['payment_id'])) {
$payment_method_data = fn_get_payment_method_data($cart['payment_id']);
}
$pp_response['order_status'] = 'K';
fn_change_order_status($order_id, $pp_response['order_status']);
//fn_print_die($order_id);

}




But there is a problem: If I put “fn_print_die($order_id);” the order status changes to status “K”. But when I remove this line the status doesn't change to “K”, it stays as it: “O”

You are setting the value in a variable that is never seen by the caller. Again, suggest you either modify the payment method script itself or use the change_order_status hook. The place_order hook is called before the payment is processed. Hence it's being overwritten when the payment processor is called.

[quote name='tbirnseth' timestamp='1417647601' post='198698']

You are setting the value in a variable that is never seen by the caller. Again, suggest you either modify the payment method script itself or use the change_order_status hook. The place_order hook is called before the payment is processed. Hence it's being overwritten when the payment processor is called.

[/quote]



Understand. Now, I am trying to use “change_order_status”. But although I am working on it about 3 days, I couldn't do it. Could you show me an examle code?

Should I use “fn_set_hook” in the fn_myaddon_change_order_status function ?



I understand that the function should looks like this:



function fn_myaddon_change_order_status($status_to, $status_from, $order_info, $force_notification, $order_statuses, $place_order) {

//** Some codes here **//
}




I searched lot but couldn't find what to put the place instead //** Some codes here **//

Sorry, I can't turn a merchant into a developer. Suggest you hire a developer with the appropriate skills to do the work you need done. It is development work.



It would also help to understand the business need for this change. I.e. what are you accomplishing with it in the operation of your business?

Hi!

You just need to utilize order_placement_routines hook in My Changes addon.



First of all, register hook in addons/my_changes/init.php:



if ( !defined('AREA') ) { die('Access denied'); }
fn_register_hooks(
'order_placement_routines'
);




Then add hook function to addons/my_changes/func.php:



if (!defined('AREA')) { die('Access denied'); }
function fn_my_changes_order_placement_routines($order_id, $force_notification, $order_info) {

$payment_method_id = 0; // change to your actual payment method;
$new_status = 'K'; // change to the letter of the desired status;

if($order_info['payment_method']['payment_id'] == $payment_method_id ) {
db_query("UPDATE ?:orders SET status = ?s WHERE order_id = ?i", $new_status, $order_info['order_id']);
}
}




Beware, that this code was taken from CS-Cart 2.2.5 and probably must be modified to futher versions.

Thank you.

Ill need to try this as we have a need to create a “Quotation” payment method. I found that it was quite easy to modify the code to support Quotations but this is the final thing needed, so that customer can select “Quotation” payment method, and the status is automatically set to “Quote” at “Place Order” event.

So just wondering if it worked ok.?

If you just use phone.tpl as the payment template, then the status of the placed order (quotation) will be Open. You could run a small script that was run from cron to look at all Open statuses and then if the payment_id matches that of your Quotation, then change the status to a different status with the appropriate email message you want to send (or not).



Alternatively you can use the 'change_order_status' hook to determine when an order is being changed from 'N' to 'O' and then check the payment method there and just update the to_status to the newer status you want. This would be the easiest and most reliable way.

Can anyone tell me how to do this with 4.3.4 version i've made as Zahhar said, but it does not work for me (((

I need to change default order status Open to some other when one of the payment method is selected and order is done by customer. I'm using phone.tpl for this payment method.

You would use the 'change_order_status' PHP hook (I wouldn't use the order_placement_routines).

I don't know your technical abilities, but I can describe how to do this briefly here.

Enable My Changes addon.

Create or edit the file func.php in app/addons/my_changes directory.

Add function like (note this is an example):

function fn_my_changes_change_order_status($&to, $from, &$order_info) {
  $payment_ids_to_act_on = array(3, 9, 15);  // get these from the actual payment methods.  Should see payment_id=xx when editing a payment
  $new_status = 'X';
if( !empty($order_info'payment_method']['payment_id']) && in_array($order_info['payment_method']['payment_id'], $payment_ids_to_act_on) )
  $to = $new_status;
}

Save the file to your server and load any page (you are checking for syntax or other errors from including this function).

Then create/edit app/addons/my_changes/init.php and have it contain:


            

Might be a profitable addon worth developing.?

The ability to configure the Order Status for each Payment Method upon completion of the payment process.

Why not to modify the payment processor itself? Or should it be modified ONLY for a payment method?

I also would like to have the option to change the default order status according to the payment method as offline payment should not have the same behaviour as online ones, is it possible already?

I also would like to have the option to change the default order status according to the payment method as offline payment should not have the same behaviour as online ones, is it possible already?

if you want to set another status for offline payment methods, please open the app/functions/fn.cart.php file and replace:

if (!$is_processor_script && $order_status == STATUS_INCOMPLETED_ORDER) {
       $order_status = 'O';
}

with

if (!$is_processor_script && $order_status == STATUS_INCOMPLETED_ORDER) {
       $order_status = 'Y';
}

where Y - is the ID of the new status. It can be found on the update status page:

http://prntscr.com/c9dnaf

Thank you very much!! :grin:

This really needs to be available from the admin side. And I think any changes like this will break if any update includes the app/functions/fn.cart.php file. Would be nice to have a a future version include this ability from admin.

This could be done by an addon payment method that didn't do anything but collect the info from whatever template you chose. You would simply set the value of the order as an addon setting and then have a simple processor script set $pp_response['order_status'] = Registry::get('addons.my_processor_addon.order_status'); Where the 'order_status' setting would be a selector of all available order statuses. No need to modify the core at all.

Take a couple of hours to put it together and test. This is the niceset solution and gives you a chance to change your mind on order status in the future.

Alternatively, you could use the 'change_order_status' hook and if the $to_status is 'O' and $order_info['payment_id'] is empty, then this would be assumed to be an offline processor (no payment_id) and you would simply adjust the $to_status to whatever you want. This would be done in your my_changes addon or other addon area.

You should avoid making core code changes wherever possible. The system provides many hooks and other methods to let you do so.

That's the nice thing about cs-cart. There are always many ways to skin the cat.