Page 1 of 2

Problem with what looks like a hook problem with newly created module

Posted: Sat Jun 21, 2025 11:13 am
by 14Steve14
Using Phoenix 1.0.9.0 PHP8.0

I am trying to create a page that shows back in stock products. Nothing too complicated. I have the page created in the root folder of the site. I have added in a new database column in the products table which shows the date that stock was updated. If I manually enter a date in tis column for a product the item shows in the new page.

The problem I am having is with the hook which is located in includes.hooks/admin/catalog. The product updates but nothing is entered into the new column.

Here is the hook code that I am using. can anyone see why this does not work.

Code: Select all

<?php
/*
  Hook: Update products_restocked_date when stock increases from 0
*/

class hook_admin_catalog_back_in_stock {

  public function __construct() {}

  public function update() {
    $this->checkRestock();
  }

  protected function checkRestock() {
    global $db, $products_id;

    if (isset($products_id, $_POST['products_quantity']) && is_numeric($products_id)) {
      $new_quantity = (int)$_POST['products_quantity'];

      $current = $db->query("SELECT products_quantity FROM products WHERE products_id = " . (int)$products_id)->fetch_assoc();

      if ((int)$current['products_quantity'] == 0 && $new_quantity > 0) {
        $db->query("UPDATE products SET products_restocked_date = NOW() WHERE products_id = " . (int)$products_id);
      }
    }
  }
}
Can anyone see anything glaringly obvious with this as to why the database table will not be added to.

Re: Problem with what looks like a hook problem with newly created module

Posted: Sat Jun 21, 2025 12:44 pm
by raiwa
I think you are missing a parentesis around the whole first $db … query before applying->fetch_assoc()

Re: Problem with what looks like a hook problem with newly created module

Posted: Sat Jun 21, 2025 2:49 pm
by ecartz
That's not a hook file, as it doesn't have any listeners. You need functions named listen_somethingWithAHook where somethingWithAHook appears in the file. Presumably you want to listen to one or more of the automatic action updates.

Re: Problem with what looks like a hook problem with newly created module

Posted: Sun Jun 22, 2025 8:31 am
by 14Steve14
My fault posted the wrong file. Here is hopefully the correct file

Code: Select all

<?php
/*
  Hook: Update products_restocked_date when stock increases from 0
*/

class hook_admin_catalog_back_in_stock {

  public function __construct() {}

  public function preAction() {
    // Store old quantity before update
    $GLOBALS['old_quantity'] = null;

    if (isset($_GET['pID']) && is_numeric($_GET['pID'])) {
      $products_id = (int)$_GET['pID'];

      $q = $GLOBALS['db']->query("SELECT products_quantity FROM products WHERE products_id = $products_id");
      if ($row = $q->fetch_assoc()) {
        $GLOBALS['old_quantity'] = (int)$row['products_quantity'];
        $GLOBALS['products_id'] = $products_id;
      }
    }
  }

  public function update() {
    $this->checkRestock();
  }

  protected function checkRestock() {
    global $db;

    if (!isset($GLOBALS['products_id'], $_POST['products_quantity'])) {
      return;
    }

    $old_quantity = (int)$GLOBALS['old_quantity'];
    $new_quantity = (int)$_POST['products_quantity'];
    $products_id = (int)$GLOBALS['products_id'];

    if ($old_quantity === 0 && $new_quantity > 0) {
      $db->query("UPDATE products SET products_restocked_date = NOW() WHERE products_id = $products_id");
    }
  }
}

Re: Problem with what looks like a hook problem with newly created module

Posted: Sun Jun 22, 2025 8:37 am
by burt
add

Code: Select all

listen_
in front of

Code: Select all

preAction()
change

Code: Select all

update()
to

Code: Select all

listen_updateProductAction()
Obviously untested, but thse changes will at least get you further.

Re: Problem with what looks like a hook problem with newly created module

Posted: Sun Jun 22, 2025 10:26 am
by raiwa
Syntax in this part of the query is wrong:

Code: Select all

 products_id = $products_id");
You can’t just include a php variable in this way.

Re: Problem with what looks like a hook problem with newly created module

Posted: Sun Jun 22, 2025 10:51 am
by 14Steve14
burt wrote: Sun Jun 22, 2025 8:37 am Obviously untested, but thse changes will at least get you further.
Gary, thanks for this. I have been playing with AI to create what I thought would be a simple task, but at the end of the day it needed a Gary to get it right. (no offense intended and hopefully none taken). I have had AI make small changes to areas on a page, but never actually tried to create anything so it was really a learning project for both me and AI I think.

I now have a working hook that allows a page to show any products which are back in stock, so items from zero back to something, which should delete automatically after 14 days. Lets hope that bit works. Then may be worth trying to get any updated products with a stock increase to show. Something to play with later and if it works may be worth installing on my live site.

Re: Problem with what looks like a hook problem with newly created module

Posted: Sun Jun 22, 2025 10:53 am
by 14Steve14
raiwa wrote: Sun Jun 22, 2025 10:26 am Syntax in this part of the query is wrong:

Code: Select all

 products_id = $products_id");
You can’t just include a php variable in this way.
Just so I learn, how should it be done?

Re: Problem with what looks like a hook problem with newly created module

Posted: Sun Jun 22, 2025 11:57 am
by raiwa

Code: Select all

  products_id = " . $products_id);

Re: Problem with what looks like a hook problem with newly created module

Posted: Sun Jun 22, 2025 2:14 pm
by 14Steve14
raiwa wrote: Sun Jun 22, 2025 11:57 am

Code: Select all

  products_id = " . $products_id);
Made the change and everything still works. many thanks @raiwa