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 );
}
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 );
}
Share
Improve this question
edited May 30, 2017 at 19:21
fwho
asked May 30, 2017 at 18:57
fwhofwho
1091 gold badge2 silver badges11 bronze badges
4
|
1 Answer
Reset to default 3This 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" );
$post_id
which you can use to get the isbn and other details from the product. – Prasun Jajodia Commented May 30, 2017 at 19:06