1

The end goal here is a plugin so that when an item is added or updated in WooCommerce I call a custom function to retrieve additional data from an API. For example, if I wanted to add the ISBN 1119327776 I would create a new product with that SKU, and then when I click publish I would call the Amazon Product Advertising API to collect and insert title, description, dimensions, etc. The problem is I am getting stuck on where and how to actually add the action. I have tried:

//test wp_update_post add_action hook
function update_test() {
    echo "this was a success!";
}

add_action( "wp_update_post", "update_test" );

//test save_post add_action hook
function update_test() {
    echo "this was a success!";
}

add_action( "save_post", "update_test" );

//test save_post_product add_action hook
function update_test() {
    echo "this was a success!";
}

add_action( "save_post_product", "update_test" );

I have tried this in both the theme function files and as a custom plugin, but with no success. However, using wp_update_post did create an output when I clicked "Add product", and not when I click "Publish" on that new product.

Update: the following code does indeed reset the title, however it times out before doing so. As if it is trying to loop all the products and update titles.

add_action( "save_post_product", "make_api_call" );
function make_api_call( $post_id ) {
    $datas["ID"]         = $post_id;
    $datas["post_title"] = "this title was reset";
    wp_update_post( $datas );
}
  • I think the third one should be enough. It can take arguments such as $post_id which you can use to get the isbn and other details from the product. – Prasun Jajodia May 30 '17 at 19:06
  • You are right, but it is also acting funny. I updated the question, but this is a start in the right direction. – fwho May 30 '17 at 19:23
  • Since you're updating the post, the function runs again and again. You have to remove the action before updating the post and add it back again after the post updates – Prasun Jajodia May 30 '17 at 19:25
  • You can find more info here codex.wordpress.org/Function_Reference/… – Prasun Jajodia May 30 '17 at 19:26
0

This code does in fact work. In the question I created an edit that talked about it timing out. That was due to the infinite loop that was created. The following code fixes that problem.

function make_api_call( $post_id ) {
    //unhook to avoid infinite loop
    remove_action( "save_post_product", "make_api_call" );

    //make API call and pass data into update array
    $update["ID"]           = $post_id;
    $update["post_title"]   = "this title was reset";

    //update the post
    wp_update_post( $update );

    //re-hook the disabled function
    add_action( "save_post_product", "make_api_call" );
}

add_action( "save_post_product", "make_api_call" );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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