内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>woocommerce offtopic - How do you add a custom function to addupdate product?
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - How do you add a custom function to addupdate product?

programmeradmin0浏览0评论

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
  • 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 Commented May 30, 2017 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 Commented May 30, 2017 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 Commented May 30, 2017 at 19:25
  • You can find more info here codex.wordpress/Function_Reference/… – Prasun Jajodia Commented May 30, 2017 at 19:26
Add a comment  | 

1 Answer 1

Reset to default 3

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" );
发布评论

评论列表(0)

  1. 暂无评论