Reputation: 1
I want to update all the product prices in woo-commerce from the custom field per gram/kg. e.g I added price to the custom field 2000. And I have selected the 2-gram weight of the product from the variations. The 2 grams should multiply by 2000 and update the product price.
Simple as that I want to control all the product prices globally. when I will change the price from the custom field all the product prices will be changed depending on grams/kg.
Upvotes: 0
Views: 329
Reputation: 21
you can use this code ida :
add_action('admin_init' , 'name_the_func');
function name_the_fun(){
global $product;
$product_weight = $product->get_weight();
$product_id = $product->get_id();
if($product_id != '' || $product_id !=0){
$price = get_the_field('afc-field',$product_id);
$product_final_price = $product_weight * $price; // ex: 40$ * 2kg
update_post_meta($product_id , '_regular_price' , $product_final_price );
}
}
Upvotes: 1