skip to Main Content

I’m pretty new to Magento custom modules and just want to be pointed in the right direction to read up on things.

I’m creating a few websites that I want to have stock dynamically updated from an API (that I’m developing).

Currently I have a script that runs every day to update every product with the current stock count, and another script that gets the difference since the last update and does it (every 10minutes).

I don’t like it as there is still room for error and it just doesn’t sit right with me. What I would like is as you click on the product, it makes an API call, updates my custom field, and renders the page. I also have a custom stock status plugin so I really need it to do the call before page load. . I can write some logic on timeout to render the page anyway if there is a problem with the API.

Any pointers would be really helpful.

2

Answers


  1. Chosen as BEST ANSWER

    Got it working... Thanks for your help, there is still some work to do, but its calling the script at the correct time! Thankyou

    Events.xml

    <?xml version="1.0"?>
    
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="catalog_controller_product_init_after">
            <observer name="Apicall" instance="OliscoTpcconnectorObserverApiCall" />
        </event>
    </config>
    
    
    
    <?php
    namespace OliscoTpcconnectorObserver;
    
    use MagentoFrameworkEventObserver as EventObserver;
    use MagentoFrameworkEventObserverInterface;
    
    class ApiCall implements ObserverInterface
    {
    
        public function execute(EventObserver $observer)
        {
          #Code here to execute!
        }
    }
    
    
    

  2. You can use the Event/Observer feature to call the API. Try one of the following events:-

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