skip to Main Content

I’m running the code bellow via cron jobs in order to disable out-of-stock products from been displayed on my web site. Could anyone who knows the prestashop’s database structure helps me to add an exception for 2 product categories with id 2 & 5 so the script dose not disable products from those categories?

<?php
  require_once('./../config/config.inc.php');
  $sql = "UPDATE "._DB_PREFIX_."product_shop SET visibility='none' WHERE id_product IN (SELECT id_product FROM "._DB_PREFIX_."stock_available WHERE quantity<=0)";
   if (!Db::getInstance()->execute($sql)) die('error!');
?>

Thanks in advance for any help

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out. This is the code in case someone else wants to use it. It works with Prestashop 8 and hides the out-of-stock products except the products assigned to category 5.

    <?php
    require_once('./../config/config.inc.php');
    
        $sqlDisable = "UPDATE "._DB_PREFIX_."product_shop SET visibility='none' WHERE id_product IN (SELECT id_product FROM "._DB_PREFIX_."stock_available WHERE quantity<=0) AND id_product NOT IN (SELECT id_product FROM "._DB_PREFIX_."category_product WHERE id_category =5)";
        $sqlEnable = "UPDATE "._DB_PREFIX_."product_shop SET visibility='both' WHERE id_product IN (SELECT id_product FROM "._DB_PREFIX_."stock_available WHERE quantity>0) AND id_product NOT IN (SELECT id_product FROM "._DB_PREFIX_."category_product WHERE id_category =5)";
    
        $resultSqlDisable = Db::getInstance()->execute($sqlDisable);
            if (!$resultSqlDisable) {
                echo "Error disabling products: " . Db::getInstance()->getMsgError();
            }
    
        $resultSqlEnable = Db::getInstance()->execute($sqlEnable);
            if (!$resultSqlEnable) {
                echo "Error enabling products: " . Db::getInstance()->getMsgError();
            }
    ?>
    

  2. You have to adjust your query by adding a WHERE condition to check in ps_category_product table which id_product(s) are not in your id_categories list.

    If you want just check by predefined/default product categories, you can just check id_default_category field in ps_product_shop.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search