将WooCommerce订单产品名称保存到用户元自定义字段 [英] Save WooCommerce Order Product Name to User Meta Custom Field
问题描述
注意:我是一名自由设计师,不是开发人员;-)
Caveat: I'm a solo freelance designer not a developer ;-)
我已经在Wordpress用户元数据中创建了一个名为<$ c $的自定义字段c> membership 。
I've created a custom field in the Wordpress user meta called membership
.
我尝试使用以下代码将WooCommerce产品名称保存为 membership
结帐此答案的帮助下的自定义字段。
I've tried the following code to save the WooCommerce Product Name to the membership
custom field on checkout with help from this answer.
已更新尝试:
function wascc_woocommerce_checkout_update_user_meta_membership ( $customer_id, $posted ) {
if (isset($posted['$orderid'])) {
$order = $posted['$orderid'];
}
$theorder = new WC_Order( $order );
$items = $theorder->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
}
if (!(empty($product_name))) {
update_user_meta( $customer_id, 'membership', $product_name);
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'wascc_woocommerce_checkout_update_user_meta_membership', 10, 2 );
它不会产生错误,但是不会将产品名称保存为 membership
。
It produces no error, but does not save the product name to membership
.
感谢帮助。
推荐答案
正如我已经评论过的那样,您应该使用 woocommerce_checkout_update_order_meta
。
as I've commented out, you should use woocommerce_checkout_update_order_meta
.
类似这样的事情:
function wascc_woocommerce_checkout_update_user_meta_membership ( $order_id ) {
$theorder = new WC_Order( $order_id );
$items = $theorder->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
}
if (!(empty($product_name))) {
// Gets the customer/user ID associated with the order. Guests are 0.
$customer_id = $theorder->get_user_id();
update_user_meta( $customer_id, 'membership', $product_name );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'wascc_woocommerce_checkout_update_user_meta_membership' );
我对您的foreach循环有疑问,虽然...您循环只是为了获得最后一个项目?
I have doubts with your foreach loop though... you are looping just to get the last item?
这篇关于将WooCommerce订单产品名称保存到用户元自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!