0

I am wanting to run some code that makes a request on product save, separate to the page product save happens on.

I have correctly set up an observer that listens to the 'catalog_product_save_after' event and makes the request.

However, now when I save a product, the page waits for the observer to finish making the request before it continues and loads the 'success' page. What practice/functionality can I use such that the product save page defers the request, but doesn't need to wait on the result to continue.

Edit:

Here is the controller code that saves the product and redirects with the success message:

            if (isset($data['variations-matrix'])) {
            foreach ($data['variations-matrix'] as $matrixdata) {
                    $simpleids[] = $this->saveproduct($data, 'simple', $matrixdata, $blankarray = []);
                }
                    $product_id = $this->saveproduct($data, 'configurable', $blankarray = [], $simpleids);
            } else {
                $product_id = $this->saveproduct($data, $data['product_type']);
            }
            $this->messageManager->addSuccess(__('The Product has been saved.'));
            return $this->_redirect('marketplace/index/products');

Without an observer, the success message and redirect happen almost instantly. With an observer listening to catalog_product_save_after however, the redirect and message only happen once the request is run. I am looking for a way for the request to be triggered without product save waiting for the request to finish.

2
  • When you make an after plugin you are technically still executing the save process; so it makes sense that it waits the plugin to end before making the success ? I might not have understood what you really aiming for.
    – Claims
    Commented Oct 14, 2021 at 13:05
  • @Claims, You do seem to understand, thanks for the response. My understanding of events and observers then is not great. Do you know of a way to trigger the plugin/request when the save happens, but without being tied to the save process?
    – Ben Dodson
    Commented Oct 14, 2021 at 13:44

2 Answers 2

0

Main process execution does not wait for the observer execution when that is dispatch.

So if your plan is about wait until your code is not finished then you 2 options here.

  1. Around Plugin (Preferred way)
  2. Override the file by creating preference.
1
  • Hi Dhiren. So you are saying that normal behavior would be that the product save should finish even if the observer is still running? This is not the behavior I am experiencing.
    – Ben Dodson
    Commented Oct 14, 2021 at 13:55
0

Assuming I understand correctly you just want to allow your code to continue running after returning control back to the caller, i.e. running asynchronously.

There are a couple options you have here:

  • There may be some optimisations you can make so that the code returns quicker,
  • Create a new message-queue + consumer. Publish the task to the message queue, which will be picked up by your consumer and execute (IMHO the best option),
  • Create a custom table, write required data to it, then have a cron task that reads from it and processes the rows in the table. This is essentially a roll-your-own message-queue.
1
  • I am planning on going with a message-queue and consumer. However, I have not done any work with queues before. I can't seem to find resources on queuing a message programmatically. Any help?
    – Ben Dodson
    Commented Oct 15, 2021 at 8:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.