Adding a custom function in 1.0.8.3

Open to all! Ask other shopowners for help.
Post Reply
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Adding a custom function in 1.0.8.3

Post by Portman »

Hi I used to have a custom function in the general.php file;

Code: Select all

  function tep_get_products_avail($product_id, $configuration_id ) {
  
    $product_query = tep_db_query("SELECT id FROM product_group_ex WHERE products_id = " . (int)$product_id . " AND configuration_id = " . (int)$configuration_id);
    $product = tep_db_fetch_array($product_query);

     if (tep_db_num_rows($product_query)) {
        return false;
     }

    return true;
Obviously things have changed in the way functions are now handled in 1.0.8.3 could someone help me out by explaining the change (in layman's terms please) and give me an idea of how I would apply the above function in the new setup - the function is only accessed from files in a template folder.

Thanks heaps,


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Adding a custom function in 1.0.8.3

Post by ecartz »

Ever since the autoloader was added, adding a custom function has been as simple as embedding it in a class. If you stick it in includes/classes or includes/system/override, it will get autoloaded when called. If both file and class are named all lower case with underscores between words, it will just work. E.g. in file class_name.php

Code: Select all

class class_name {

  public static function method_name($product_id, $configuration_id) {
    return !mysqli_num_rows($GLOBALS['db']->query(sprintf(<<<'EOSQL'
SELECT id FROM product_group_ex WHERE products_id = %d AND configuration_id = %d LIMIT 1
EOSQL
    , (int)$product_id, (int)$configuration_id)));
  }

}
Call like

Code: Select all

class_name::method_name($product_id, $configuration_id)
You'll have to work out what class_name and method_name should be yourself. For example, it could be product_group_ex::has_available. But there isn't enough context to tell if that makes sense.
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: Adding a custom function in 1.0.8.3

Post by Portman »

Thanks for this @ecartz ...

Can I ask another question regarding general functions...

I had modified the old function tep_get_products_stock so that I could count stock on some items and not others by having a switch on each product "products_count_stock" and it checked the switch and if the item did not have to count stock it would report a massive stock figure 10000 rather than the actual stock figure (which by default was set to 0) for non-counting stock... this is the modified function:

Code: Select all

 function tep_get_products_stock($products_id) {
    $products_id = tep_get_prid($products_id);
//    $stock_query = tep_db_query("SELECT products_quantity FROM products WHERE products_id = " . (int)$products_id);
	$stock_query = tep_db_query("SELECT products_quantity, products_count_stock FROM products WHERE products_id = " . (int)$products_id); // Mod By Peter - for stock count modification
    $stock_values = $stock_query->fetch_assoc();
// Added by Peter - Stock Count Mod
	    if($stock_values['products_count_stock'] != 1)
    {
	$stock_values['products_quantity'] = 10000;
    }												   
// End Added	 
    return $stock_values['products_quantity'];
  }
I have had a bit of a look around at the new structure to try and find the new function that checks the stock figures but I am not having any luck... can you point me in the right direction, and in the name of not changing the core code is there a better way that I should be doing this? I still cannot get my head around all the ins and outs of it all.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Adding a custom function in 1.0.8.3

Post by ecartz »

It's the

Code: Select all

$product->get('in_stock')
I don't really see a better direct way of handling it than overriding the Product class or replacing the stock checking code (remove the database hook and add your own).

If you are just looking for a better way of implementing your hack, consider adding a hook after the stock update during checkout that says

Code: Select all

$GLOBALS['db']->query("UPDATE products SET products_quantity = 10000 WHERE products_count_stock != 1");
Also, add the same listener to the insertProductAction and updateProductAction hooks on the catalog page in admin.

My quick thought is that it would probably be easiest to modify the stock checking code.

https://github.com/CE-PhoenixCart/Phoen ... _stock.php
https://github.com/CE-PhoenixCart/Phoen ... _stock.php

You may possibly have to add a column to the products in the order class using the cartOrderProductColumns hook.
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: Adding a custom function in 1.0.8.3

Post by Portman »

Thanks so much for that @ecartz your suggestions really helped simplify how I was approaching this issue!!!

Can I ask another question related to this subject... As I am trying to set up the site to recognize some products as counting stock and some as not counting stock I have created a switch in the products table...

products_count_stock; value 0 = Don't count stock
products_count_stock ; value 1 = Do Count Stock.

I think I have it working everywhere but on the shopping cart page...

Products that don't need to be counted presently have a big red cross next to them - I want this to be a big green tick

I am playing around with the includes/modules/content/shopping_cart/templates/tpl_cm_sc_product_listing.php file... but cannot work out how to upload the products_count_stock value from the database into the file so that I can create an if statement to get over this problem...

If I want to make the variable $stock_count equal the value for the product stored in products_count_stock how do I do that??
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Adding a custom function in 1.0.8.3

Post by ecartz »

Update to 1.0.8.7 and say

Code: Select all

$stock_count = $product->get('count_stock');
If products_count_stock is on the products table, that should just work on the shopping cart page, inside the loop.
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: Adding a custom function in 1.0.8.3

Post by Portman »

Thanks for that.. it may take me a little while to get there but when I do I'll give it a go!!


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