• Resolved Anonymous User 14808221

    (@anonymized-14808221)


    Hi

    We are using Linnworks on a WooCommerce project.

    We have code in place that is hooked to the save_post() hook and whenever we update a product, it performs some custom logic, and then updates a custom meta we added to the Products.

    This works smoothly of course, but as soon as the product is “updated” by Linnworks, our code, hooked to save_post() does not fire anymore.

    We reached out to Linnworks and they say:

    we don’t access WooCommerce backend directly like that (using save_post() action) – we call it via the API.
    http://woocommerce.github.io/woocommerce-rest-api-docs/#update-a-product
    The request we send sets managing_stock to true, and sets stock_quantity to the quantity.

    With this they explain the fact that save_post() is not triggered, but this makes no sense to us.
    As far we know save_post is triggered by any REST request updating any post. How else would REST update a post, if not thru save_post, even if it is not accessed directly?

    After all, save_post is fired at the end of wp_insert_post() which is the core function that’s run whenever a post is inserted or updated (wp_update_post() calls it internally). This includes when the post is updated via the classic editor and the block editor (Gutenberg), as well whenever it’s updated via the REST API. The only reason it wouldn’t fire is if the post was being updated via SQL directly (via a plugin or otherwise), or when only post meta is updated via a function.

    So, my question is:

    Is it true that when any external service such as Linnworks updates a Product thru the REST API, save_post() is NOT fired?
    And, how would we then “listen” to the updating process of that external service?
    It MUST be possible to somehow detect when a product is updated, wether it is updated with a REST or PHP call should really not matter!

    We would really appreciate if WooCommerce could shed some light into this, because to us it seems very much wrong that a “REST Api call does not trigger save_post” like Linnworks states, and yet we can see with our own eyes that our hooks do not work whenever a product is updated by them, so there must be some truth to it, however in that case, please could you instruct how we can detect a update process happening in REST?

    We need to hook our own code logic to each update post event no matter if its thru REST or backend.

    Thanks a lot

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @bedas,

    If you haven’t yet, I would suggest asking this question in the WooCommerce Slack Community or in the Facebook group. Those are typically better spots to ask development-oriented questions.

    Thread Starter Anonymous User 14808221

    (@anonymized-14808221)

    I have asked in slack at no avail.
    There’s no chance a question like above will be replied by other users on a channel like slack given the amount of other, much more trivial questions popping in that channel like hourly almost 😅

    I’ll try Facebook and if there’s no success there either I’ll guess until found.

    Still a tad disappointed wc support can’t just tell me what to listen to when anything uses their REST api so I can fire my custom php whenever Rest updated the posts/products…

    Hey @bedas,

    Thanks for giving that a try. Slack can be tough as things get lost in the backscroll. Also, since it’s a holiday weekend in the US that will affect who’s around too.

    I did run a few quick tests on the REST API. I used this code snippet to trigger an email each time save_post is run.

    
    function my_project_updated_send_email( $post_id ) {
     
        // If this is just a revision, don't send the email.
        if ( wp_is_post_revision( $post_id ) ) {
            return;
            }
     
        $post_title = get_the_title( $post_id );
        $post_url = get_permalink( $post_id );
        $subject = 'A post has been updated';
     
        $message = "A post has been updated on your website:\n\n";
        $message .= $post_title . ": " . $post_url;
     
        // Send email to admin.
        wp_mail( 'admin@example.com', $subject, $message );
    }
    add_action( 'save_post', 'my_project_updated_send_email' );
    

    Source: https://developer.wordpress.org/reference/hooks/save_post/#div-comment-3368

    In my testing, save_post was not triggered when I only updated manage_stock and stock_quantity. However, if I included the name of the product, then save_post would be triggered. I suspect we have to change something in the _posts table like the “title” in order to trigger that action.

    I then duplicated that function and switched the action to update_post_meta. Now when I send a request to only update manage_stock and stock_quantity an email will trigger.

    Try switching to update_post_meta and see if it works for you.

    Thread Starter Anonymous User 14808221

    (@anonymized-14808221)

    @3sonsdevelopment – thank you so much for your time and input!

    This is great news and confirms something I probably should have realized on my own. I actually posted this on the first message above 🙄:
    The only reason it wouldn’t fire is if the post was being updated via SQL directly (via a plugin or otherwise), or when only post meta is updated via a function.

    So that’s confirmed as well with your findings

    Save post is fired only if post data is manipulated but not when postmeta is manipulated which is what happens when linnworks – or any rest api – updates the stock only, without touching the actual product (post) data

    I’ll be testing this ASAP on the project itself and confirm here the results

    Thanks again so much for guiding me and your testing – which I know isn’t done “quickly”
    I truly appreciate it 🙏🙏

    Nicola Mustone

    (@nicolamustone)

    Automattic Happiness Engineer

    Save post is fired only if post data is manipulated but not when postmeta is manipulated which is what happens when linnworks – or any rest api – updates the stock only, without touching the actual product (post) data

    Thanks for finding that out and sharing the knowledge!

    I’ll be testing this ASAP on the project itself and confirm here the results

    Please, do let us know if you find anything interesting! We’ll keep the thread open for a while!

    Thread Starter Anonymous User 14808221

    (@anonymized-14808221)

    Coming back here to confirm the solution is working.

    I used the updated_meta_type_meta hook, which permits to listen to a specific meta type update, and using the $meta_id parameter we can target specific meta ID.

    Example code targeting post meta:

    add_action( 'updated_post_meta', 'meta_save_post_action', 10, 4 );
    function meta_save_post_action( $meta_id, $object_id, $meta_key, $_meta_value ) {
    if($meta_key == 'meta_key_to_listen'){
    //do things
    }
    }

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Does the REST API trigger save_post or not?’ is closed to new replies.