简体   繁体   中英

Get user custom meta data value and update it in WooCommerce orders

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);

Since Woocommerce 3, your code is a bit outdated and you don't need to use any SQL query. So I have revisited your code a bit, using some WC_Data methods on the available WC_Order Object.

Now additionally, in this function, we get the meta value from user meta key "_kontostandaktuell" and then update that meta value, adding to it, your calculated value.

The code:

add_filter('woe_get_order_value_Kontostandeintrag', 'filter_get_order_value_kontostandeintrag', 10, 3 );
function filter_get_order_value_kontostandeintrag( $value, $order, $fieldname ) {
    // Calculation from different order meta values
    $value  = $order->get_total() +
              $order->get_meta('GS-Bargeldeinzahlung') +
              $order->get_meta('GS-Mitgliedsbeitrag') +
              $order->get_meta('GS-Nachlass') +
              $order->get_meta('GS-Guthabenkonto');

    // Update order meta value for "Kontostandeintrag" key
    $order->update_meta_data('Kontostandeintrag', $value );
    $order->save(); // Save to database

    // Get the User ID from order
    $user_id = $order->get_customer_id();

    // Get user meta value for "_kontostandaktuell" key
    $kontostandaktuell = get_user_meta( $user_id, '_kontostandaktuell', true );

    // Update user meta value for "_kontostandaktuell" key
    update_user_meta( $user_id, '_kontostandaktuell', $kontostandaktuell + $value );

    return $value;
}

It should work.

Related WordPress user functions documentation: get_user_meta() and update_user_meta()


Addition (related to your comment) :

To reset _kontostandaktuell user field before, you should just removefrom the code:

// Get user meta value for "_kontostandaktuell" key
$kontostandaktuell = get_user_meta( $user_id, '_kontostandaktuell', true );

And remove from update_user_meta() function $kontostandaktuell + , so you will have instead:

update_user_meta( $user_id, '_kontostandaktuell', $value );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM