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

Open to all! Ask other shopowners for help.
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

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

Post 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.


Join The Code Co-op to get access to your library in the Code Co-op Forum
raiwa
Certified Developer
Posts: 1640
Joined: Sat Dec 21, 2019 8:08 am
Phoenix Version: 1.1.0.6
Has thanked: 70 times
Been thanked: 152 times

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

Post by raiwa »

I think you are missing a parentesis around the whole first $db … query before applying->fetch_assoc()
Public Phoenix Change Log Cheat Set on Google Sheets
https://docs.google.com/spreadsheets/d/ ... sp=sharing

Need Help?viewtopic.php?f=10&t=27
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

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

Post 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.
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

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

Post 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");
    }
  }
}
User avatar
burt
Core Team
Posts: 4551
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

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

Post 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.
I am not here to build for you.
I am here to build with you. Let's help each other.
raiwa
Certified Developer
Posts: 1640
Joined: Sat Dec 21, 2019 8:08 am
Phoenix Version: 1.1.0.6
Has thanked: 70 times
Been thanked: 152 times

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

Post 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.
Public Phoenix Change Log Cheat Set on Google Sheets
https://docs.google.com/spreadsheets/d/ ... sp=sharing

Need Help?viewtopic.php?f=10&t=27
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

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

Post 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.
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

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

Post 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?
raiwa
Certified Developer
Posts: 1640
Joined: Sat Dec 21, 2019 8:08 am
Phoenix Version: 1.1.0.6
Has thanked: 70 times
Been thanked: 152 times

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

Post by raiwa »

Code: Select all

  products_id = " . $products_id);
Public Phoenix Change Log Cheat Set on Google Sheets
https://docs.google.com/spreadsheets/d/ ... sp=sharing

Need Help?viewtopic.php?f=10&t=27
14Steve14
Senior Contributor
Posts: 923
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

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

Post 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


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