How do you add a custom function to add / update product?

Question

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 );
}
0
fwho 2 years 2020-01-01T08:38:22-05:00 0 Answers 91 views 0

Leave an answer

Browse
Browse