Page 1 of 1

Further Help with Count Stock on some items mod

Posted: Sat Apr 20, 2024 4:42 am
by Portman
Hi,

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.