php 通过在Prestashop中的cron作业隐藏缺货产品

luaexgnf  于 2023-09-29  发布在  PHP
关注(0)|答案(2)|浏览(85)

我通过cron作业运行下面的代码,以便禁用在我的网站上显示的缺货产品。任何人谁知道prestashop的数据库结构可以帮助我添加一个例外2产品类别与id 2和5,所以脚本不禁用这些类别的产品?

<?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!');
?>

提前感谢任何帮助

vojdkbi0

vojdkbi01#

您必须通过添加WHERE条件来调整查询,以检查ps_category_product表中哪些id_product不在您的id_categories列表中。
如果您只想按预定义/默认的商品类别进行检查,可以在ps_product_shop中检查id_default_category字段。

fae0ux8s

fae0ux8s2#

我想通了。这是代码,以防其他人想要使用它。它与Prestashop 8一起工作,并隐藏除分配给类别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();
        }
?>

相关问题