1

I am assigning each product in WooCommerce to an author. Each author, when they sign up, adds in a short bio and this is saved with the author's user account. People are signing up with Profile Press.

I would like to insert this bio field into each product this author creates but I can seem to figure it out. Thanks in advance.

2
  • 1
    You did not specify where the user field is saved to (meta key), or where you want it inserted in the product
    – sMyles
    Commented Feb 19, 2019 at 22:40
  • WooCommerce and other 3rd party plugin/theme dev support is off topic and not in this stacks scope. You should ask via their official support routes or in their groups and communities.
    – Tom J Nowell
    Commented Jun 24, 2021 at 8:37

1 Answer 1

0

You can do this with an action when a new product is inserted (and not when updated). As you did not specify the meta key this field is stored under the user as, OR the meta key of where you want it stored on the product:

add_action( 'wp_insert_post', 'smyles_insert_user_product_data', 10, 3 );

function smyles_insert_user_product_data( $post_ID, $post, $update ){

    if( $update || $post->post_type !== 'product' ){
        return;
    }

    $user_id = get_current_user_id();

    if( $user_id ){
        $short_bio = get_user_meta( $user_id, 'some_meta_key', true );
        if( ! empty( $short_bio ) ){
            update_post_meta( $post_ID, 'product_meta_key', $short_bio );
        }
    }
}

This assumes that the "short bio" is stored in the user's meta, so you need to figure out that meta key and replace some_meta_key in the code above with correct one for the user.

You then also need to define what meta key you want to use to set that value in the product with, and replace product_meta_key above.

To find the user meta key used, you can use my User Meta Display plugin: https://wordpress.org/plugins/user-meta-display/ (it's a bit out-dated but still works)

UPDATE:

To update the description (post_content) before data in inserted into the database use this instead:

add_action( 'wp_insert_post_data', 'smyles_insert_user_product_data', 10, 2 );

function smyles_insert_user_product_data( $data, $postarr ){

    // Non-empty ID means it's an update (don't want to modify on update)
    if( ! empty( $postarr['ID'] ) || ! array_key_exists( 'post_type', $postarr ) || $postarr['post_type'] !== 'product' ){
        return $data;
    }

    $user_id = get_current_user_id();

    if( $user_id ){
        $short_bio = get_user_meta( $user_id, 'some_meta_key', true );
        if( ! empty( $short_bio ) ){
            $short_bio = wp_kses( $short_bio, true );
            $data['post_content'] = wp_slash( $short_bio );
        }
    }

    return $data;
}
12
  • Thanks so much for the response. How can I find the corresponding meta-key for that bio field?
    – Z Crow
    Commented Feb 21, 2019 at 4:14
  • @ZCrow it's a little bit out-dated but still works, you can use my User Meta Display plugin to figure out what the user meta key is wordpress.org/plugins/user-meta-display
    – sMyles
    Commented Feb 21, 2019 at 16:32
  • Thanks for the plugin suggestion. That gave me what I was looking for.
    – Z Crow
    Commented Feb 22, 2019 at 16:37
  • Still can't get this to go. I appreciate your patience. So, the user meta key is called "description". The product meta key I can use is "description" as well (I'm using the short description for the product's description). Here is my code:
    – Z Crow
    Commented Feb 22, 2019 at 17:44
  • function smyles_insert_user_product_data( $post_ID, $post, $update ){ if( $update || $post->post_type !== 'product' ){ return; } $user_id = get_current_user_id(); if( $user_id ){ $short_bio = get_user_meta( $user_id, 'description', true ); if( ! empty( $short_bio ) ){ update_post_meta( $post_ID, 'description', $short_bio ); } } }
    – Z Crow
    Commented Feb 22, 2019 at 17:45

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