Page 1 of 1

Help with stock count Mod

Posted: Fri May 24, 2024 12:20 am
by Portman
Hi,

I posted this request for help a while back in the standard community forum, but never got any traction... probably because I was asking too much... anyway after a bit of a break to focus on other things in my business I now have time to look at the website again...

Any help that could be offered would be appreciated...


I created a mod that allowed me to count stock on some items only, because some items we order in and some we make to order... I had it working up until I updated PHP to 8.0 then it stopped working.... I asked earlier about an issue I had that was throwing an error ... but this is another issue which is causing me to be in a loop at checkout ... basically I go from shopping cart - to shipping and then get forced back into shopping cart.

I have assumed that the error lies somewhere in the following bits of code that I have modified/created but i cant work it out....

in the file originally in includes/system/versioned1.0.7.other/1.0.7.12/products.php I have changed the lacks stock function from;

Code: Select all

    public function lacks_stock($quantity = null) {
      return $this->get('in_stock') < ($quantity ?? $this->get('quantity'));
    }
to;

Code: Select all

    public function lacks_stock($quantity = null) { // Modified to only count stock on some items only


		  $stock_count_value = $GLOBALS['db']->query("SELECT products_count_stock FROM products WHERE products_id = " . (int)$this->get('id'))->fetch_assoc();	
			if ($stock_count_value['products_count_stock'] == "1") {		  
				if ($this->get('in_stock') < $this->get('quantity')) {
					$quantity = 1;
				}
			}		
      return $quantity;
    }
this is just a simple change that only checks stock on items that have the products_count_stock switch set to '1', I had something else that worked before the update but I had to change it to this to have the shopping cart work'

then I have added the following hook to includes/hooks/shop/checkout/CheckStock.php

Code: Select all

<?php
class hook_shop_checkout_CheckStock {
	
	function listen_startCheckout() {

		  if (STOCK_CHECK === 'true') {
    $stock_count = "SELECT products_count_stock FROM products WHERE products_id =%d"; 
    $any_out_of_stock = false;
    foreach ($order->products as $product) {
	  $product_id = Product::build_prid($product['id']);
	  $stock_count_value = $GLOBALS['db']->query(sprintf($stock_count, (int)$product_id))->fetch_assoc();	// Added by peter to only count stock on some items
	  if ($stock_count_value['products_count_stock'] == '1'){
		if (product_by_id::build($product_id))->lacks_stock($product['qty'])) {
			$any_out_of_stock = true;
		}
	  } 
    }

    // Out of Stock
    if ( $any_out_of_stock && (STOCK_ALLOW_CHECKOUT !== 'true') ) {
      Href::redirect($GLOBALS['Linker']->build('shopping_cart.php'));
    }
  }
		
	}
	
}	
?>
And here (I assume) is where i am getting redirected back to the shopping cart page - but I can't work out what is wrong - It looks ok to me... It redirects me regardless of if I am purchasing a Non Count stock item, or if I am purchasing a count stock item that I have enough of in stock.

Any thoughts would be really helpful.

Re: Help with stock count Mod

Posted: Fri May 24, 2024 1:03 am
by ecartz
Try

Code: Select all

if ($this->get('count_stock') == '1') {
  return $this->get('in_stock') < ($quantity ?? $this->get('quantity'));
} else {
  return false;
}
and then just use the regular stock checking code otherwise (i.e. get rid of your hook).

Re: Help with stock count Mod

Posted: Fri May 24, 2024 6:29 am
by Portman
Thanks @ecartz that solved the loop - and it is working now....

If I can ask one more thing...

I have kept the hook for now as it's main function was to only deduct an order from products_quantity for items that needed stock to be counted(Ie count_stock = 1).

however this is obviously messy as to make this hook work I have to have admin->configuration->stock->subtract Stock set to False (otherwise it subtracts stock twice fror count_stock items and once for non count_stock items)

What would be the propper way to do this?

Re: Help with stock count Mod

Posted: Fri May 24, 2024 9:17 am
by ecartz
That hook does not reduce stock at all. For just checking stock, you don't need to make changes beyond the lacks_stock method.

Find the one that does in core. https://github.com/CE-PhoenixCart/Phoen ... _stock.php

Create a replacement method somewhere with the logic you want (copy the original and edit the copy). Presumably you want to add products_count_stock to the $stock_sql query and change empty($stock_values['is_virtual']) to empty($stock_values['products_count_stock'])

Change the database entry for the core hook to point at your new method instead of the original. https://github.com/CE-PhoenixCart/Phoen ... x.sql#L794

Re: Help with stock count Mod

Posted: Tue May 28, 2024 5:25 am
by Portman
thanks for this,

I have created a new method based on the one you pointed out to me and I have it working now...

So to make it NOT change the core code - I save it in the same folder under a new name;

eg includes/system/segment/checkout/count_limited_stock.php

what I dont get is how I then change the database entry for the core hook so it points to the new file./

Re: Help with stock count Mod

Posted: Tue May 28, 2024 9:11 am
by ecartz
phpMyAdmin?

https://github.com/CE-PhoenixCart/Phoen ... .sql#L1165

Change Checkout to checkout_surface. Change update_stock to count_limited_stock

Re: Help with stock count Mod

Posted: Wed May 29, 2024 1:26 am
by Portman
Thankyou so much for the help on this one @ecartz,
And sorry if I seem a bit slow on with this but I seem to have it all working now, can you just confirm that what I have done is correct and best practice in this sort of situation;

1. Modified 'includes/system/segments/checkout/update_stock.php' and saved it in the same folder as 'count_limited_stock.php'

2. Copied includes/system/versioned/1.0.7.8/checkout.php to includes/system/override/... folder and added the following function to the bottom;

Code: Select all

public static function count_limited_stock() {
     		 require 'includes/system/segments/checkout/count_limited_stock.php';
    } 
3. changed the hook_method field in the hooks database that you highlighted (ID 55) from 'update_stock' to 'count_limited_stock'

Re: Help with stock count Mod

Posted: Wed May 29, 2024 1:53 am
by ecartz
Seems rather roundabout. Just change the class from Checkout to checkout_surface, and it will be able to find count_limited_stock. That's what the surface classes do. They let you load files underneath their paths. You shouldn't need to override Checkout.

In general, you should name things based on what they do. This code updates the stock in a more limited fashion. So I would tend to call it update_stock_limited rather than count_stock_limited. It should function either way, but you asked about best practices.

Re: Help with stock count Mod

Posted: Wed May 29, 2024 4:05 am
by Portman
thanks @ecartz

Note taken about the name of the file...

I'm not sure what you mean about changing the class but as it works as is should I just leave it or is this something important to fix up? if its important to fix could you explain it a bit as I have not been able to find any further info on the checkout/checkout_surface classes