Page 1 of 1

save session variable to orders table with a Hook

Posted: Sat Jul 23, 2022 6:03 pm
by loop
Hi all

i made a hook for saving the referer to the orders table and i copied the code from a other module and it worked :)
the only thing is, i'm not sure if every 3 functions are needed to only save the parameter to the DB or is one of them to use it from the DB. Thank you for clarifying:

Code: Select all

    function listen_databaseOrderBuild($parameters) {
        if (!empty($parameters['builder']->get('orders_advertiser'))) {
            $order_info = &$parameters['order']->info;
            $order_info['orders_advertiser'] = $parameters['builder']->get('orders_advertiser');
        }
    }

    function listen_constructOrder($parameters) {
        if (!empty($_SESSION['orders_advertiser'])) {
            $order_info = &$parameters->info;
            $order_info['orders_advertiser'] = ($_SESSION['orders_advertiser']);
        }
    }

    function listen_insertOrder($parameters) {
        $orders = &$parameters['sql_data']['orders'];
        $orders['orders_advertiser'] = ($GLOBALS['order']->info['orders_advertiser'] ?? '');
    }
Maybe be first not?

thank in advance for helping / telling me

Re: save session variable to orders table with a Hook

Posted: Sat Jul 23, 2022 7:24 pm
by ecartz
Just

Code: Select all

    function listen_insertOrder($parameters) {
        $orders = &$parameters['sql_data']['orders'];
        $orders['orders_advertiser'] = $_SESSION['orders_advertiser'] ?? '';
    }
is sufficient to insert it into the database. Both the other methods are about adding it to the order object, either during checkout or after. Note that there is also a bug in the constructOrder one. If orders_advertiser is set in the session during subsequent invocations (after checkout), it will change it on the order object to that value. It won't save it, so probably not a very impactful bug. But unnecessary if you are never accessing it through the order object.

Re: save session variable to orders table with a Hook

Posted: Mon Jul 25, 2022 5:40 am
by loop
thank you ecartz, it worked!