1

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 2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ummm, like using a lock file. So, I assume the actionUpdateQuantity hook is called while my module is still running. Using this trick the module knows that is still running and exit. I thought the hook was called after my module ends. I will try it!
I was testing and had counter to see how many times it got called and think it was 2. But could not find reason why. Maybe something to do with hookActionProductSave but did not found other solution.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.