Page 1 of 1

Hook to insert after Order is inserted in DB

Posted: Wed Aug 24, 2022 6:14 am
by loop
Hi All
i need to insert in a separat table data after the order is inserted in the db (to be specific, i want on special payment methods where i have the payment imediately to insert the amount i received in a specific table)

i saw the hook listen_insertOrder but i think thats specific for the orders table....

thanks in advance!

Re: Hook to insert after Order is inserted in DB

Posted: Wed Aug 24, 2022 7:06 am
by ecartz
https://github.com/CE-PhoenixCart/Phoen ... .sql#L1166

Code: Select all

INSERT INTO hooks (hooks_site, hooks_group, hooks_action, hooks_code, hooks_class, hooks_method) VALUES ('shop', 'checkout_process', 'startCheckout', '_14_insert_order', 'checkout_surface', 'insert_order');
That's the hook that invokes the order insertion.

I wouldn't describe listen_insertOrder as being limited to the order table, but it does run before the insertion. If you want to add a new table and use the order ID, I would suggest adding a hook that runs after _14_insert_order -- you can get the order ID by $GLOBALS['order']->get_id() E.g.

Code: Select all

class hook_shop_checkout_process__14a_after_insert {

  public function listen_startCheckout() {
    $order_id = $GLOBALS['order']->get_id();
    // do stuff here
  }

}

Re: Hook to insert after Order is inserted in DB

Posted: Wed Aug 24, 2022 9:03 am
by loop
thank you eCartz, i think your hook is what i need but i'm not sure about the naming of the file.

if i put it in
templates\override\includes\hooks\shop\siteWide\ what is the name of the file?
with the content of this

Code: Select all

class hook_shop_checkout_process__14a_after_insert {

  public function listen_startCheckout() {
    $order_id = $GLOBALS['order']->get_id();
    // do stuff here
  }

}

Re: Hook to insert after Order is inserted in DB

Posted: Wed Aug 24, 2022 9:06 am
by ecartz
hooks/shop/checkout_process/_14a_after_insert.php

Re: Hook to insert after Order is inserted in DB

Posted: Wed Aug 24, 2022 10:12 am
by loop
perfect, it worked!

is there maybe a simpler solution to get payment_method / amount without query the database? maybe in the orders object?

Code: Select all

public function listen_startCheckout() {
        $order_id = $GLOBALS['order']->get_id();
        $payment_method = tep_db_get_one_field('payment_method','orders','orders_id="'.(int)$order_id.'"');
        $amount = tep_db_get_one_field('value','orders_total','orders_id="'.(int)$order_id.'" AND class = "ot_total"');
        
        //Betrag bereits einbuchen
        if(($payment_method=="Vorauszahlung") || ($payment_method=="Kredit Karte") || ($payment_method=="Twint") || ($payment_method=="PayPal") || ($payment_method=="PostFinance Debit Direct (Postcard)")){
           tep_db_query("INSERT INTO orders_paid (orders_id, paid, amount, `time`, who) values ('".$order_id."','1','".$amount."','".time()."','Order Import')");
        }
    }

Re: Hook to insert after Order is inserted in DB

Posted: Wed Aug 24, 2022 11:09 am
by ecartz
Considering that you just inserted them into the database, go back and look at insert_order.php and see what it did to fetch those values (so as to insert them). Then do that.

Re: Hook to insert after Order is inserted in DB

Posted: Wed Aug 24, 2022 12:02 pm
by loop
perfect, thank you!