In woocommerce, I'm trying to add additional code to a existing php code, that stores an value to an advanced customer field in the db.
As you can see I calculate the value "Kontostandeintrag" as a result of an addition of different values and store this value in the post_meta in the Field "Kontostandeintrag". I do this for each order line.
This works fine.
As next step I would like to read the existing customer field "_kontostandaktuell" (for the customer of the order line)in the user_meta, add the actual value "Kontostandeintrag" to this field and update the field "_kontostandaktuell" again with the sum value of this fields. So after run through all order lines, I should have the sum value of all "Kontostandeintrag" values in the user_meta field "_kontostandaktuell" for each customer.
Get user custom meta data value and update it in WooCommerce
The existing code which I like to extend is:
add_filter('woe_get_order_value_Kontostandeintrag', function( $value, $order, $fieldname ) {
$id = $order->get_id();
$value =get_post_meta($id,"GS-Bargeldeinzahlung",true) + $order->get_total() + get_post_meta($id,"GS-Mitgliedsbeitrag",true) + get_post_meta($id,"GS-Nachlass",true) + get_post_meta($id,"GS-Guthabenkonto",true);
global $wpdb;
$data = array("meta_value" =>$value);
$where = array("post_id"=>$id,"meta_key" =>"Kontostandeintrag");
$wpdb->update( "wp_postmeta", $data, $where );
return $value;
},10,3);