save session variable to orders table with a Hook

Open to all! Ask other shopowners for help.
Post Reply
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

save session variable to orders table with a Hook

Post 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


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: save session variable to orders table with a Hook

Post 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.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: save session variable to orders table with a Hook

Post by loop »

thank you ecartz, it worked!


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply