Jump to content

Automatically deactivate/activate Products out of/in stock


Loxx

Recommended Posts

I was looking for a solution to activate/deactivate products that get out of stock or come back in stock...

Vekia has a nice solution with triggers here:

https://mypresta.eu/en/art/tips-and-tricks/how-to-disable-out-of-stock-product.html

Which i took to write a php skript, that I am executing as a Cron Job on the server... It is a little faster then the triggers and more reliable.

Here is the code:

 

<?php
ini_set('display_errors', 1);
$mysql_host = 'ENTER DB HOST';
$mysql_database = 'ENTER DB NAME';
$mysql_user = 'ENTER DB USER';
$mysql_password = 'ENTER PASSWORD';
 
$db = new PDO("mysql:host=$mysql_host;dbname=$mysql_database", $mysql_user, $mysql_password);
 
 
$sql    = 'UPDATE ps_product_shop SET active=0 WHERE id_product IN (SELECT id_product FROM ps_stock_available WHERE quantity=0);
UPDATE ps_product_shop SET active=1 WHERE id_product IN (SELECT id_product FROM ps_stock_available WHERE quantity>0);';
 
$stmt = $db->prepare($sql);
 
 
 
if ($stmt->execute())
     echo "Success";
else 
     echo "Fail";
 
?>
 
It basically runs the update SQL Query on the specified DATABASE.
 
Hope it is useful for someone out there...
cheers
 

product_stock_availability.php

Link to comment
Share on other sites

Thanks for sharing this. Another way would be just to set the avaliable_for_order to zero. This way the products still remain on the front page but can't be ordered anymore. The SQL to do that looks like this:

$sql    = 'UPDATE ps_product_shop SET avaliable_for_order=0 WHERE id_product IN (SELECT id_product FROM ps_stock_available WHERE quantity=0);
UPDATE ps_product_shop SET avaliable_for_order=1 WHERE id_product IN (SELECT id_product FROM ps_stock_available WHERE quantity>0);';

This way the products also don't get lost by search engines.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...