Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

switch status when out of stock when allow checkout = true


trogette

Recommended Posts

I've been working on making a contribution that will give an option on individual products to automatically change the status when it goes out of stock when allow checkout is set to 'true.' I've got as far as adding the necessary field to the products table and altering categories.php to update the status-switch status (if that makes sense! lol) and that works OK. (I've also added the option at category and global level but haven't done anything about that affecting the products yet. I haven't got as far as figuring out how to make them override each other in the right way, which would be category overrides global and product overrides category and global)

 

Now I'm looking in checkout_process.php at how the status gets changed when 'allow checkout' is set to false:

if ( ($stock_left < 1) && (STOCK_ALLOW_CHECKOUT == 'false') ) {
	  tep_db_query("update " . TABLE_PRODUCTS . " set products_status = '0' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
	}

and thinking that it should be relatively straightforward to tweak this to do what I want. I came up with this:

if ( ($stock_left < 1) && (STOCK_ALLOW_CHECKOUT == 'true') ) {
	  $status_query = tep_db_query("select products_status_switch from " . TABLE_PRODUCTS . " where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
	  if ($status_query == 1) {
	  tep_db_query("update " . TABLE_PRODUCTS . " set products_status = '0' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
	  }
	}

but it doesn't have any effect. Am I missing an incidence of STOCK_ALLOW_CHECKOUT == 'true' somewhere else that totally overrides checkout_process.php?

Link to comment
Share on other sites

  • 1 month later...
Uhm... bump?? :)

 

Just so I follow what you are trying to do.... You want to make it seletable for a single product so that when it goes out of stock they wont be able to order it any more? So that if you had 1 single item for sale, 2 people would be unable to purchase it while still allowing out of stock ordering for all the other items on your site?

 

I'm kinda hoping this is the case because it is something I realized yesterday could be pretty handy.

Link to comment
Share on other sites

The question is when it goes out of the stock. In the checkout process is too late. You can have multiple customers with products in their cart so you can still be out of stock and having orders coming through. If you update the stock when adding items to the cart the changes will take effect immediately but you will have problems with abandoned carts. So you would have to run some scripts to restore the stock before deleting the carts etc. And again in that case someone can put your entire store with every product as "out of stock".

 

A possible solution will be to check the cart during the checkout confirmation right when the final form is submitted but before the order is recorded. And that will require plenty of mods for the checkout process.

Link to comment
Share on other sites

David, yes that's sort-of what I'm trying to do :) I have some stuff that's hand made, one-of-a-kind and some stuff that's drop-shipped and some that I order in when I have to (ie when I get an order for something I don't have on my hands.) What I want is to not have to manually set the one-of-a-kind products' status to 'off' when they're sold, but I don't want to set a stock level on the drop-shipped items and would like to keep an accurate stock level (via QTPro) on the others.

 

Mark, yes that does concern me but I figure it's something for further development. My market audience is pretty forgiving when it comes to emailing and explaining that the product they wanted isn't available, want to cut back on this but atm figure it's not completely avoidable, though unlikely as my traffic/order rate isn't that high! Given all that, how come my simple change-of-status routine isn't working??

Link to comment
Share on other sites

Is not working because the order is through when your run your code. The stock is updated in checkout_process in this loop

 

  for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
// Stock Update - Joao Correia

 

So this code retrieves info from the order object already in the database and after the payment gateway is through. It is the reason I mentioned to do it on the final confirmation page. Problem is you have to change a lot of code there (about the form processing) to make it work with every payment module.

Edited by enigma1
Link to comment
Share on other sites

But the original piece of code in my OP *does* work and I put my code before it and after it just to see if there was a difference, so why would that work and not mine? The status has to be changed after the stock has updated, how can it be otherwise?

Link to comment
Share on other sites

if you just want to always switch the status you change this:

 

if ( ($stock_left < 1) && (STOCK_ALLOW_CHECKOUT == 'false') ) {

 

to this

if ( $stock_left < 1) {

 

this will always update the product status. But that's not what I was talking about. I was refering to the stock control with carts having the product in them and yet the product status is <=0.

 

Now if the above works for what you're trying to do make sure the extra switch you provided products_status_switch is updated for each product. Then you would do:

$status_query = tep_db_query("select products_status_switch from " . TABLE_PRODUCTS . " where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
$status_switch = tep_db_fetch_array($status_query);
	if ( ($stock_left < 1) && $status_switch['products_status_switch'] ) {
tep_db_query("update " . TABLE_PRODUCTS . " set products_status = '0' where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
}
}

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for the pointers, I had a fish about in other files for STOCK_ALLOW_CHECKOUT and I can see where I'm going with it now :) Now I need to have a way to selectively change the status-trigger field en masse...

Link to comment
Share on other sites

  • 3 weeks later...

I've just come back to this and I'm getting a 'unexpected $end' error in checkout_process on a line number beyond the end of the file. Thoughts? I want to get this working with the switch selection on the product level at least then upload it while I (or anyone else!) work on the category-level stuff.

Link to comment
Share on other sites

The question is when it goes out of the stock. In the checkout process is too late. You can have multiple customers with products in their cart so you can still be out of stock and having orders coming through. If you update the stock when adding items to the cart the changes will take effect immediately but you will have problems with abandoned carts. So you would have to run some scripts to restore the stock before deleting the carts etc. And again in that case someone can put your entire store with every product as "out of stock".

 

A possible solution will be to check the cart during the checkout confirmation right when the final form is submitted but before the order is recorded. And that will require plenty of mods for the checkout process.

 

 

all that would require is to put this :

 

require(DIR_WS_CLASSES . 'order.php');

$order = new order;

 

 

// Final Stock Check

if (STOCK_CHECK == 'true') {

$n=sizeof($order->products);

$any_out_of_stock = false;

for ($i=0; $i<$n; $i++) {

if (tep_check_stock($order->products[$i]['id'], $order->products[$i]['qty'])) {

$any_out_of_stock = true;

}

}

// Out of Stock

if ( (STOCK_ALLOW_CHECKOUT != 'true') && ($any_out_of_stock == true) ) {

// add a message to inform the user to your message stack

// maybe even remove the product from the cart

// in any event go back to the basket

tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));

}

}

 

just after:

 

require(DIR_WS_CLASSES . 'order.php');

$order = new order;

 

in checkout_confirmation

Treasurer MFC

Link to comment
Share on other sites

Well this is what's already in checkout_confirmation:

 

// Stock Check
 $any_out_of_stock = false;
 if (STOCK_CHECK == 'true') {
for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
  if (tep_check_stock($order->products[$i]['id'], $order->products[$i]['qty'])) {
	$any_out_of_stock = true;
  }
}
// Out of Stock
if ( (STOCK_ALLOW_CHECKOUT != 'true') && ($any_out_of_stock == true) ) {
  tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
}
 }

 

and this is what I've done with SSS:

 

//  ****stock status switch****
// Stock Check
 $any_out_of_stock = false;
 if (STOCK_CHECK == 'true') {
for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
  if (GLOBAL_STOCK_STATUS_SWITCH != 'false') {
// redirect to out_of_stock.php if 'stock status switch' is 'true'
	  $status_query = tep_db_query("select products_status from " . TABLE_PRODUCTS . " where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
	  $status = tep_db_fetch_array($status_query);
	  if ( $status == 0 ) {
		tep_redirect(tep_href_link(FILENAME_OUT_OF_STOCK));
	  }
	}
   else {
	if (tep_check_stock($order->products[$i]['id'], $order->products[$i]['qty'])) {
	  $any_out_of_stock = true;

	}
  }
 }	  
// Out of Stock
if ( (STOCK_ALLOW_CHECKOUT != 'true') && ($any_out_of_stock == true) ) {
  tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
}
 }
//  ****stock status switch - end****

 

This also means that if something is manually set to 'off' in the admin panel you get kicked back to the out of stock page.

Edited by trogette
Link to comment
Share on other sites

Well this is what's already in checkout_confirmation:

 

// Stock Check
 $any_out_of_stock = false;
 if (STOCK_CHECK == 'true') {
for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
  if (tep_check_stock($order->products[$i]['id'], $order->products[$i]['qty'])) {
	$any_out_of_stock = true;
  }
}
// Out of Stock
if ( (STOCK_ALLOW_CHECKOUT != 'true') && ($any_out_of_stock == true) ) {
  tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
}
 }

 

and this is what I've done with SSS:

 

//  ****stock status switch****
// Stock Check
 $any_out_of_stock = false;
 if (STOCK_CHECK == 'true') {
for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
  if (GLOBAL_STOCK_STATUS_SWITCH != 'false') {
// redirect to out_of_stock.php if 'stock status switch' is 'true'
	  $status_query = tep_db_query("select products_status from " . TABLE_PRODUCTS . " where products_id = '" . tep_get_prid($order->products[$i]['id']) . "'");
	  $status = tep_db_fetch_array($status_query);
	  if ( $status == 0 ) {
		tep_redirect(tep_href_link(FILENAME_OUT_OF_STOCK));
	  }
	}
   else {
	if (tep_check_stock($order->products[$i]['id'], $order->products[$i]['qty'])) {
	  $any_out_of_stock = true;

	}
  }
 }	  
// Out of Stock
if ( (STOCK_ALLOW_CHECKOUT != 'true') && ($any_out_of_stock == true) ) {
  tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
}
 }
//  ****stock status switch - end****

 

This also means that if something is manually set to 'off' in the admin panel you get kicked back to the out of stock page.

 

ah, you made it product specific with an information page, nice.

Edited by boxtel

Treasurer MFC

Link to comment
Share on other sites

ah, you made it product specific with an information page, nice.

 

as an add on you might consider also setting this at a category level.

Many people put similar products in categories and chances are that they also have the same stock strategy.

That would enable people to switch a lot of products with one go.

Treasurer MFC

Link to comment
Share on other sites

that was my original plan, thinking the simplest (from the front-end coding pov) would be to find a way to set the product status switch on all the products in the category from the category edit page, just not got round to it yet :) need to check it out with the myriad of other contributions I use on my shop...

 

think I need to add a thing on the admin product listing that shows whether the status switch is set to switch or not switch so that you know before you get to the edit page

 

or someone else could...

Edited by trogette
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...