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?
Dan
Hooks Example
- burt
- Core Team
- Posts: 4546
- Joined: Tue Oct 29, 2019 9:37 am
- Phoenix Version: v1.1.0.8
- : Buy Me A Beverage
- Has thanked: 252 times
- Been thanked: 412 times
Re: Hooks Example
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
If you append to parameters, it will go at the bottom of the infoBox.and have the output show up where I want it placed
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.
-
Dan Cole
- Senior Contributor
- Posts: 498
- Joined: Fri Oct 25, 2019 2:14 pm
- Phoenix Version: 1.0.8.21
- Has thanked: 67 times
- Been thanked: 61 times
Re: Hooks Example
@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
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
- burt
- Core Team
- Posts: 4546
- Joined: Tue Oct 29, 2019 9:37 am
- Phoenix Version: v1.1.0.8
- : Buy Me A Beverage
- Has thanked: 252 times
- Been thanked: 412 times
Re: Hooks Example
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).
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).
I am not here to build for you.
I am here to build with you. Let's help each other.
I am here to build with you. Let's help each other.
-
Dan Cole
- Senior Contributor
- Posts: 498
- Joined: Fri Oct 25, 2019 2:14 pm
- Phoenix Version: 1.0.8.21
- Has thanked: 67 times
- Been thanked: 61 times
Re: Hooks Example
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.
Dan
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 Cole
- Senior Contributor
- Posts: 498
- Joined: Fri Oct 25, 2019 2:14 pm
- Phoenix Version: 1.0.8.21
- Has thanked: 67 times
- Been thanked: 61 times
Re: Hooks Example
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 '';
}
}-
Dan Cole
- Senior Contributor
- Posts: 498
- Joined: Fri Oct 25, 2019 2:14 pm
- Phoenix Version: 1.0.8.21
- Has thanked: 67 times
- Been thanked: 61 times
Re: Hooks Example
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...
Dan
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 '';
}
}