skip to Main Content

I’m developing a simple module that hooks to actionUpdateQuantity hook. So, every time the stock of a product is updated I must update the stock of other products.

But, to update the stock I call stockAvailable object, which trigger the actionUpdateQuantity hook. So, I have a endless loop.

Then I tried to manually update the stock directly on the database using SQL, but this have the problem that other modules don’t “see” the stock updates. So, modules like MailAlert, ebay or Amazon don’t update stock correctly.

I’m a bit stuck here.

How can I update the stock without enter a loop ?

Thanks!

2

Answers


  1. I had similar issue before and think this is not best way but worked for me. Idea is to add class variable in your module:

    protected $isSaved = false;
    

    then in hookActionProductUpdate function first check that variable and later after you done saving data change its value

    public function hookActionProductUpdate($params)
    {
        if ($this->isSaved)
            return null;
        ...
        $this->isSaved = true;
    }
    
    Login or Signup to reply.
  2. Another way to do this is, in your module when you submit new quantity make sure you also submit product id and attribute id. Then in your hook you can do a check.

    public function hookActionUpdateQuantity($params) 
    {
        if ((int)Tools::getValue('id_product') != $params['id_product'] 
            || (int)Tools::getValue('id_attribute') != $params['id_product_attribute']) {
            return false;
        }
    
        // do your stuff
    }
    

    Everytime the hook actionUpdateQuantity triggers you have a $params array of product whose quantity is being updated.

    $params['id_product']           // id of a product being updated
    $params['id_product_attribute'] // id of product combination being updated
    $params['quantity']             // quantity being set to product
    

    This way your hook will run only once when you are updating quantity of a product from your module (form?). As you update other products quantity they will also trigger this hook but since the data in $params array is different than your POST’ed data, the hook method will return false.

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