How to add user id as product SKU with WP User Frontend product form.

You can follow the instructions given below to insert user id as product SKU.

1) Add a hidden field to the product form you created & insert _sku meta key to that field.

​Refer Screenshot:
http://prntscr.com/ef1e0f

2) Now, Add the following code inside the theme’s functions.php file.

Note: This is a Developer level doc. If you are unfamiliar with code/templates and resolving potential conflicts, contact me for assistance.


function wpuf_update_product_sku( $post_id ) {

if ( isset( $_POST['_sku'] ) ) {

$user_id = get_current_user_id();
update_post_meta( $post_id, '_sku', $user_id );

}

}

add_action( 'wpuf_edit_post_after_update', 'wpuf_update_product_sku' );
add_action( 'wpuf_add_post_after_insert', 'wpuf_update_product_sku' );

​​

​All done, submit a product using frontend product form & check the product SKU in wp-admin. The SKU id will be the user id.

By Mahbubur Rahman

Self Taught Programmer And Learning Enthusiast

Leave a comment

Your email address will not be published. Required fields are marked *