Page 1 of 1

Upgrading error and cleaning up sloppy programming

Posted: Sun Sep 26, 2021 7:11 am
by Portman
Hi everyone,

I am in the process of upgrading my site and in the process I am trying to clean up some sloppy programming a third party programmer did for me (before I realised that there were preferred programmers on this site that could have done the job for me)...

I added some code to the file includes\system\versioned\1.0.7.8\checkout.php back in the upgrade to version 1.0.7.10 (I think) and now as I upgrade to 1.0.7.13 it is causing me grief....

Can someone have a look at it and maybe give me an idea of where the problem would generate from and maybe tell me how I would fix it up so that it is not messing with the core code.

the code I have added (originally written by my programmer) is here;

Code: Select all

    public static function reset_cart() {
// Added by programmer
      foreach ($GLOBALS['order']->products as $product) { //This is the line generating the error 

        $stock_query = tep_db_query("SELECT products_quantity, products_count_stock FROM products WHERE products_id = '" . tep_get_prid($product['id']) . "'");
      if ($stock_values = tep_db_fetch_array($stock_query)) {

      if ($stock_values['products_count_stock'] == 1)
      {
      
        // do not decrement quantities if products_attributes_filename exists
        
          $stock_left = $stock_values['products_quantity'] - $product['qty'];
          tep_db_query("UPDATE products SET products_quantity = " . (int)$stock_left . " WHERE products_id = '" . tep_get_prid($product['id']) . "'");

      }
    }
  }// End Added by programmer
	
      $_SESSION['cart']->reset(true);
    }
The code is there to allow me to count stock movements on some items in my store and not others...

the error I am getting is;

Code: Select all

PHP Notice:  Undefined index: order in .../includes/system/versioned/1.0.7.8/checkout.php on line 194
PHP Notice:  Trying to get property 'products' of non-object in ..../includes/system/versioned/1.0.7.8/checkout.php on line 194
PHP Warning:  Invalid argument supplied for foreach() in .../includes/system/versioned/1.0.7.8/checkout.php on line 194
I tried to workout where the call to this function was coming from but cannot locate it so I am not sure where to go from here.

Re: Upgrading error and cleaning up sloppy programming

Posted: Sun Sep 26, 2021 7:34 am
by ecartz
It's called via hook. In this case, the reset pipeline. The reset pipeline is called after checkout or logoff. I'm guessing that the warning is coming during logoff, because there isn't an order then.

You could try

Code: Select all

      foreach ($GLOBALS['order']->products ?? [] as $product) {
That won't fix that code to do what you want, but it would get past the PHP warning.

You could move that into a new hook, e.g.

Code: Select all

class hook_shop_checkout_process__29_count_stock {

  public function listen_startCheckout() {
    // stuff you want to do
  }

}
or perhaps

Code: Select all

class hook_shop_after__24_count_stock {

  public function listen_afterStart() {
    // stuff you want to do
  }

}

Re: Upgrading error and cleaning up sloppy programming

Posted: Tue Oct 05, 2021 3:17 am
by Portman
thanks @ecartz