Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Created April 10, 2024 15:25
Show Gist options
  • Save ebuildy/5cdf8f702053d26f0df372582c5f0e46 to your computer and use it in GitHub Desktop.
Save ebuildy/5cdf8f702053d26f0df372582c5f0e46 to your computer and use it in GitHub Desktop.

Revisions

  1. ebuildy created this gist Apr 10, 2024.
    42 changes: 42 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    if ($sql)
    {
    $product_ids = Db::getInstance()->ExecuteS($sql);

    $beginTime = microtime(true);

    foreach ($product_ids as $product_id)
    {

    /**
    * PLICIWEB : modification de la requete dans le else if
    */
    if (version_compare(_PS_VERSION_, '1.7.2', '<') AND !Configuration::get('PMS_USE_SYNCHRONIZE'))
    $q = "UPDATE "._DB_PREFIX_."stock_available sa1 SET `quantity`=( SELECT SUM(quantity) FROM (SELECT * FROM "._DB_PREFIX_."stock_available) sa2 WHERE sa2.id_product=sa1.id_product AND sa2.id_product_attribute >0 GROUP BY sa2.id_product) WHERE sa1.id_product=".$product_id['id_product']." AND sa1.id_product_attribute=0";

    else if (version_compare(_PS_VERSION_, '1.7.2', '>=')) {
    $sqlGetQuantities = " SELECT id_product, SUM(GREATEST(0, quantity)) as q, SUM(physical_quantity) as pq
    FROM "._DB_PREFIX_."stock_available
    WHERE id_product = ".(int)$product_id['id_product'] . " AND id_product_attribute > 0 ";

    $getQuantities = Db::getInstance()->getRow($sqlGetQuantities, false);

    if (!empty($getQuantities)) {
    $sqlUpdateQuantities = "UPDATE "._DB_PREFIX_."stock_available
    SET
    `physical_quantity` = " . (int) $getQuantities['pq'] . ",
    `quantity` = " . (int) $getQuantities['q'] . "
    WHERE
    id_product = ".(int)$product_id['id_product']." AND id_product_attribute = 0";
    }

    $result = Db::getInstance()->execute($sqlUpdateQuantities, false);

    $parent_updates++;
    }
    }

    $totalTime = microtime(true) - $beginTime;

    echo 'Update ' . $parent_updates . ' parents took ' . $totalTime . ' ms' . "\n";
    fwrite($handle,"[".date("Y-m-d H:i:s")."] ".$_SERVER['QUERY_STRING'].' Update ' . $parent_updates . ' parents took ' . $totalTime . ' ms' . "\n");
    }