Page 1 of 1

Hooks Example

Posted: Fri Jul 10, 2026 2:19 pm
by Dan Cole
How about an example Gary? I'd like to add a hook to replace the hard coded Sales History button I've added to the default catalog admin infobox as shown on this page. How would I determine the listener to use and have the output show up where I want it placed?

Hook Placement.jpg

Dan

Re: Hooks Example

Posted: Fri Jul 10, 2026 3:24 pm
by burt
Dan Cole wrote: Fri Jul 10, 2026 2:19 pm How would I determine the listener to use
These "side boxes" in admin are termed infoBox

Search code for infoBox reveals a listener ready to intercept $parameters.

https://github.com/CE-PhoenixCart/Phoen ... ox.php#L22
and have the output show up where I want it placed
If you append to parameters, it will go at the bottom of the infoBox.

You therefore might use

array_splice on $parameters['content']

--

From 2 years ago, almost to the day;

https://phoenixcart.org/forum/viewtopic.php?f=10&t=2431

Seems more or less as needed, other than making the `text` your button.

Re: Hooks Example

Posted: Fri Jul 10, 2026 6:52 pm
by Dan Cole
@burt

Thanks Gary. For me that was a light bulb moment. I knew the side boxes were called infoboxes but it would never have occurred to me to look for that file. I didn't realize it was a separate component. Now I have a solid path to run on so I'll try my hand at placing that code in a hook.

Dan

Re: Hooks Example

Posted: Fri Jul 10, 2026 7:23 pm
by burt
Give it a go, I think you'll surprise yourself when you see the possibilities/flexibility, behind the curtain.
Post back if you need detailed help, it's just a bunch of jigsaw pieces.

I believe this particular hooking point should work from v1.0.8.5 (so it's been in core approx 5 yrs).

Re: Hooks Example

Posted: Sat Jul 11, 2026 4:15 pm
by Dan Cole
I gave it a go and ended up with this which I cobbled together with the help of a code monkey but the name of Gemini. The hook adds a Sales History button within the infobox beside the catalog products page in admin. You can see that button in the image contained in my first post. While I haven't posted it here the button will take you to a detailed report page that shows the customers who purchase a particular product. If anyone is interested in this I can also make that report available. You just need to PM me.

Here is the hook, with comments, so you'll understand exactly what it is doing.

Code: Select all

class hook_admin_catalog_catalogSalesHistory {
    public function listen_infoBox($parameters) {
        if (isset($parameters['contents']) && is_array($parameters['contents'])) {
            $Admin = $GLOBALS['Admin'];
            
            // Confirm the active global product context exists
            if (isset($GLOBALS['product']) && $GLOBALS['product'] instanceof Product) {
                $product = $GLOBALS['product'];
                
                // Build the sales history link
                $history_link = $Admin->link('products_sold.php')->set_parameter('products_id', $product->get('id'));

                // Build the new button block
                $button_item = [
                    'class' => 'text-center',
                    'text' => $Admin->button(
                        defined('TEXT_SALES_HISTORY') ? TEXT_SALES_HISTORY : 'Sales History', 
                        'fas fa-info-circle', 
                        'btn-info mr-2', 
                        $history_link
                    )
                ];

                // --- Official CE Phoenix array_splice Practice ---
                // We inject exactly 1 item at index 5 (after Price/Quantity, before Rating)
                // We set length to 0 so it inserts the item instead of replacing anything
                array_splice($parameters['contents'], 5, 0, [$button_item]);
            }
        }

        // Return the modified contents stack back to the template engine
        return $parameters['contents'];
    }
}
Dan

Re: Hooks Example

Posted: Sat Jul 11, 2026 5:24 pm
by Dan Cole
I moved this hook over to my development site where I had error reporting turned on, which generated a warning message so here is an update to sort that out.

Code: Select all

class hook_admin_catalog_catalogSalesHistory {
    // This targets the native 'infoBox' category broadcast from infobox.php
    public function listen_infoBox($parameters) {
        // Because infobox.php passed parameters by reference, we can mutate them directly
        if (isset($parameters['contents']) && is_array($parameters['contents'])) {
            global $product, $Admin, $PHP_SELF;

            // Secure checkpoint: Ensure we are only adding this to the product catalog view
            if (basename($PHP_SELF) == 'catalog.php' && isset($product) && ($product instanceof Product)) {
                
                // Build the sales history link safely using core objects
                $history_link = $Admin->link('products_sold.php')->set_parameter('products_id', $product->get('id'));

                // Structure the button array element
                $button_item = [
                    'class' => 'text-center',
                    'text' => $Admin->button(
                        defined('TEXT_SALES_HISTORY') ? TEXT_SALES_HISTORY : 'Sales History', 
                        'fas fa-info-circle', 
                        'btn-info mr-2', 
                        $history_link
                    )
                ];

                // Inject using array_splice at index 5 (after Price/Qty, before Rating)
                // This updates the reference variable array that infobox.php loops through to build the HTML table rows!
                array_splice($parameters['contents'], 5, 0, [$button_item]);
            }
        }
        
        // Return nothing (or an empty string) to keep the hook processor free of string-conversion warnings
        return '';
    }
}

Re: Hooks Example

Posted: Sat Aug 15, 2026 8:10 pm
by Dan Cole
I back ported this hook for my older 1.0.8.20 site. If differs from the 1.1.06 version in a number of ways but I won't bore you with the details....if anyone whats to use either of the versions and would also like the report page that it links to, to show customers who purchased a particular product let me know and I'll package it up for you. Here is the 1.0.8.20 hook...

Code: Select all

class hook_admin_catalog_catalogSalesHistory {
    public function listen_infoBox($parameters) {
        global $PHP_SELF;

        // 1. Target admin/catalog.php and check for product ID in URL
        if (basename($PHP_SELF) == 'catalog.php' && isset($_GET['pID'])) {
            global $Admin;

            $pID = (int)$_GET['pID'];

            // 2. Build the link using 1.0.8.20 supported link syntax
            $history_link = $Admin->link('products_sold.php', 'products_id=' . $pID);

            // 3. Fallback button markup compatible with 1.0.8.20 Bootstrap/FontAwesome styles
            $button_html = '<a href="' . $history_link . '" class="btn btn-info mr-2">' .
                '<i class="fas fa-info-circle"></i> ' . 
                (defined('TEXT_SALES_HISTORY') ? TEXT_SALES_HISTORY : 'Sales History') . 
                '</a>';

            $button_item = [
                'class' => 'text-center',
                'text'  => $button_html
            ];

            // 4. Inject into contents parameter array
            if (isset($parameters['contents']) && is_array($parameters['contents'])) {
                array_splice($parameters['contents'], 4, 0, [$button_item]);
            }
        }
        
        return '';
    }
}
Dan