Assuming your products are custom posts you can do it like this:
add_action('wp_insert_post', 'do_stuff'); // when product is created
add_action('save_post', 'do_stuff' ); // on product update
function do_stuff( $post_id ) {
// Making sure this runs only for products
$slug = 'product_post';
if ( $slug != $_POST['post_type'] ) {
return;
}
if (!get_post_meta( $post_id, 'new_price', true )){
$old_price = get_post_meta( $post_id, 'old_price', true );
update_post_meta($post_id, 'new_price', $old_price);
}
}
Update old products
Let’s update them by 50.
// url: site.com/test-page?offset=0, change this by 50 (0,50,100,150,200,250...)
$offset = $_GET['offset']; // start with 0
$args = [
'offset' => $offset,
'posts_per_page' => 50,
'post_type' => 'product_post'
];
$the_query = new WP_Query( $args );
if($the_query->posts){
foreach($the_query->posts as $product){
if (!get_post_meta( $product->ID, 'new_price', true )){
$old_price = get_post_meta( $product->ID, 'old_price', true );
update_post_meta($product->ID, 'new_price', $old_price);
}
}
}else{
echo 'Done!';
}