Search code examples
prestashophookprestashop-1.6

Prestashop - Which SQL called after actionProductUpdate?


I try to update a name from ps_product when I update my product's stock. I added a SQL request on the hook actionProductUpdate.

The hook works, because when I set a die; after the SQL, the database is updated.

But I don't know where, another SQL is called and set back to the old value. I use PROFILING and I saw that it's called after....

How can I set this call as the last thing to do after a product update save ?

This is the simplified code :

public function hookActionProductUpdate($params)
{
    $product = $params['product'];

    $sql = 'UPDATE ' . _DB_PREFIX_ . 'product mp 
            SET mp.name = ''
            WHERE mp.id_product = ' . $product->id;

    Db::getInstance()->execute($sql);

}

PS : If I change a another parameter like reference, this time it works !


Solution

  • It is better to use object product to achieve your goal. Something like

    public function hookActionObjectProductUpdateAfter($params)
    {
        $product = new Product($params['object']->id);
        foreach(Language::getLanguages(true) as $language) {
            $product->name[$language['id_lang']] = 'new name';
        }
        $product->update();
    }
    

    in this case, you don't care where the name is persisted in the DB, and you will never damage it

    PS : Take care of params if you want to get 'product'. In actionProductUpdate, it's params['product'] but in hookActionObjectProductUpdateAfter it's params['object'] (as Product). I lost so much time on this...