how to write 301 redirect code for woocommerce product

If you already created a new product you can write directly in your product template before get_header()

global $post;
if($post->ID===$your_old_product_ID){
  wp_redirect(site_url('/product/product-name/'),301);
}

But it will be better if you create some custom field in products to manage their redirects.