[英]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.在 woocommerce 中,我正在尝试向现有的 php 代码添加其他代码,该代码将值存储到数据库中的高级客户字段。
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".如您所见,我通过添加不同的值来计算值“Kontostandeintrag”,并将该值存储在字段“Kontostandeintrag”的 post_meta 中。 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.作为下一步,我想读取 user_meta 中现有的客户字段“_kontostandaktuell”(对于订单行的客户),将实际值“Kontostandeintrag”添加到该字段并再次使用总和值更新字段“_kontostandaktuell”这个领域的。 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.因此,在遍历所有订单行之后,我应该在 user_meta 字段“_kontostandaktuell”中为每个客户提供所有“Kontostandeintrag”值的总和值。
Get user custom meta data value and update it in WooCommerce获取用户自定义元数据值并在 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.由于 Woocommerce 3,您的代码有点过时,您不需要使用任何 SQL 查询。 So I have revisited your code a bit, using some WC_Data
methods on the available WC_Order
Object.所以我重新审视了你的代码,在可用的WC_Order
Object 上使用了一些WC_Data
方法。
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.现在另外,在这个 function 中,我们从用户元键“_kontostandaktuell”中获取元值,然后更新该元值,将计算值添加到它。
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()
相关 WordPress 用户函数文档: get_user_meta()
和update_user_meta()
Addition (related to your comment) :添加(与您的评论有关) :
To reset _kontostandaktuell
user field before, you should just removefrom the code:要在之前重置_kontostandaktuell
用户字段,您应该从代码中删除:
// 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()
function $kontostandaktuell +
中删除,因此您将拥有:
update_user_meta( $user_id, '_kontostandaktuell', $value );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.