phpwordpresscustom-fieldswordpress-hookpost-meta

WordPress : how add new post meta before saving product woocommerce


i have a api from another wocommerce website and take some information with my product sku how can i add this information to post meta after created new product ? i wanna when i create a product , the information take with api and product sku and save to post meta.

i found this hook but i think this not working

<?PHP
function my_product_att( $sku ) {
   // my api codes and take information and use      
   add_post_meta('product_id','key','value');
   // i haven't problem here i just need Appropriate hook 
}
add_action( 'save_post', 'my_product_att' ); // this hook not work for woocommerce

Solution

  • Woocommerce Products are wordPress posts. You can use wordpress hooks like save_post & $post_id as its argument. You are passing $sku which is wrong.

    add_action( 'save_post', 'wpse_110037_new_posts' );
    function wpse_110037_new_posts($post_id){
        $post_type = get_post_type($post_id);
        if($post_type == 'products') {
            add_post_meta($post_id,'key','value');
        }
    }