I'm creating a custom plugin for my website.
我正在为我的网站创建一个自定义插件。
In some part of this plugin I need to store extra meta in wp_postmeta
for each orders.
在这个插件的某些部分,我需要在每个订单的wp_postmeta中存储额外的元数据。
I added this in my plugin's class:
我在我的插件类中添加了这个:
`add_action ('woocommerce_before_checkout_process', array( &$this, 'add_item_meta', 10, 2) );`
And this is add_item_meta()
function:
这是add_item_meta()函数:
function add_item_meta( $item_id, $values ) {
wc_add_order_item_meta($item_id, '_has_event', 'yes' );
}
This function is not complete, but nothing happens with this codes; I think I need to use another hook but I can't find a proper one.
此功能不完整,但此代码没有任何反应;我想我需要使用另一个钩子,但我找不到合适的钩子。
Does anyone know anything about this?
有人对这个有了解吗?
I also have another problem with $item_id
: this is woocommerce global variable but I can't see it in my plugin!
$ item_id还有另一个问题:这是woocommerce全局变量,但我在插件中看不到它!
I mean I don't have access to this variable from my plugin or something like this!
我的意思是我无法从我的插件或类似的东西访问这个变量!
4 个解决方案
#1
18
The 2018 way:
2018年的方式:
Built on Guido W.P. answer you can use instead woocommerce_checkout_create_order
action hook in a more lighter and effective version code (using WC 3+ CRUD methods):
建立在Guido W.P.回答你可以使用更轻,更有效的版本代码中的woocommerce_checkout_create_order动作钩子(使用WC 3+ CRUD方法):
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {
$order->update_meta_data( '_custom_meta_key', 'value' );
}
Code goes in function.php file of your active child theme (or active theme).
代码位于活动子主题(或活动主题)的function.php文件中。
Tested and works in WooCommerce 3+ (only).
经测试并适用于WooCommerce 3+(仅限)。
SOME EXPLANATIONS:
The woocommerce_checkout_create_order
action hook is just one step before saving the order data. See below in an extract of the WC_Checkout
create_order()
method (with both hooks):
woocommerce_checkout_create_order操作挂钩只是保存订单数据之前的一步。请参阅下面WC_Checkout create_order()方法的摘录(包含两个钩子):
/**
* Action hook to adjust order before save.
* @since 3.0.0
*/
do_action( 'woocommerce_checkout_create_order', $order, $data );
// Save the order.
$order_id = $order->save();
do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data );
return $order_id;
Why using
woocommerce_checkout_create_order
instead?:为什么要使用woocommerce_checkout_create_order?:
- Because You don't need to use
$order = wc_get_order( $order_id );
as you already got$order
as an argument in the hooked function.因为您不需要使用$ order = wc_get_order($ order_id);因为你已经将$ order作为钩子函数中的参数。
- You don't need to use
$order->save();
as this will be done just after anyway (see the source code)您不需要使用$ order-> save();因为这将在完成之后完成(参见源代码)
- Also
woocommerce_checkout_create_order
has been released in WooCommerce version 3 and it's maid for that too.此外,woocommerce_checkout_create_order已经在WooCommerce版本3中发布,它也是佣人。
So this just works with a single line of code inside the function.
所以这只是在函数内部使用一行代码。
#2
16
Building on Mo Saeedi answer, I believe this snippet is more in line with the new CRUD approach introduced by WooCommerce 3.0:
基于Mo Saeedi回答,我相信这个片段更符合WooCommerce 3.0引入的新CRUD方法:
add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {
$order = wc_get_order( $order_id );
$order->update_meta_data( 'my_custom_meta_key', 'my data' );
$order->save();
} , 10, 2);
See also this threads on the WordPress forums:
另请参阅WordPress论坛上的这些主题:
- Oh CRUD! Custom Meta to order from cart
- What is the correct way to update meta fields in Woocommerce?
哦CRUD!自定义Meta从购物车订购
在Woocommerce中更新元字段的正确方法是什么?
#3
4
answer is: I should use woocommerce_checkout_update_order_meta
for add_action and also i should simply use update_post_meta()
to add extra meta to my order
答案是:我应该使用woocommerce_checkout_update_order_meta进行add_action,我也应该使用update_post_meta()为我的订单添加额外的元数据
function add_item_meta( $order_id ) {
//global $woocommerce;
update_post_meta( $order_id, '_has_event', 'yes' );
}
#4
3
The 2016 way:
2016年方式:
add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {
update_post_meta( $order_id, 'my_custom_meta_key', 'my data' );
} , 10, 2);
-
$order_id
is the id of the order, which is stored as a custom post type -
$posted
is all the data from$_POST
$ order_id是订单的ID,存储为自定义帖子类型
$ posted是来自$ _POST的所有数据
#1
18
The 2018 way:
2018年的方式:
Built on Guido W.P. answer you can use instead woocommerce_checkout_create_order
action hook in a more lighter and effective version code (using WC 3+ CRUD methods):
建立在Guido W.P.回答你可以使用更轻,更有效的版本代码中的woocommerce_checkout_create_order动作钩子(使用WC 3+ CRUD方法):
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {
$order->update_meta_data( '_custom_meta_key', 'value' );
}
Code goes in function.php file of your active child theme (or active theme).
代码位于活动子主题(或活动主题)的function.php文件中。
Tested and works in WooCommerce 3+ (only).
经测试并适用于WooCommerce 3+(仅限)。
SOME EXPLANATIONS:
The woocommerce_checkout_create_order
action hook is just one step before saving the order data. See below in an extract of the WC_Checkout
create_order()
method (with both hooks):
woocommerce_checkout_create_order操作挂钩只是保存订单数据之前的一步。请参阅下面WC_Checkout create_order()方法的摘录(包含两个钩子):
/**
* Action hook to adjust order before save.
* @since 3.0.0
*/
do_action( 'woocommerce_checkout_create_order', $order, $data );
// Save the order.
$order_id = $order->save();
do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data );
return $order_id;
Why using
woocommerce_checkout_create_order
instead?:为什么要使用woocommerce_checkout_create_order?:
- Because You don't need to use
$order = wc_get_order( $order_id );
as you already got$order
as an argument in the hooked function.因为您不需要使用$ order = wc_get_order($ order_id);因为你已经将$ order作为钩子函数中的参数。
- You don't need to use
$order->save();
as this will be done just after anyway (see the source code)您不需要使用$ order-> save();因为这将在完成之后完成(参见源代码)
- Also
woocommerce_checkout_create_order
has been released in WooCommerce version 3 and it's maid for that too.此外,woocommerce_checkout_create_order已经在WooCommerce版本3中发布,它也是佣人。
So this just works with a single line of code inside the function.
所以这只是在函数内部使用一行代码。
#2
16
Building on Mo Saeedi answer, I believe this snippet is more in line with the new CRUD approach introduced by WooCommerce 3.0:
基于Mo Saeedi回答,我相信这个片段更符合WooCommerce 3.0引入的新CRUD方法:
add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {
$order = wc_get_order( $order_id );
$order->update_meta_data( 'my_custom_meta_key', 'my data' );
$order->save();
} , 10, 2);
See also this threads on the WordPress forums:
另请参阅WordPress论坛上的这些主题:
- Oh CRUD! Custom Meta to order from cart
- What is the correct way to update meta fields in Woocommerce?
哦CRUD!自定义Meta从购物车订购
在Woocommerce中更新元字段的正确方法是什么?
#3
4
answer is: I should use woocommerce_checkout_update_order_meta
for add_action and also i should simply use update_post_meta()
to add extra meta to my order
答案是:我应该使用woocommerce_checkout_update_order_meta进行add_action,我也应该使用update_post_meta()为我的订单添加额外的元数据
function add_item_meta( $order_id ) {
//global $woocommerce;
update_post_meta( $order_id, '_has_event', 'yes' );
}
#4
3
The 2016 way:
2016年方式:
add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {
update_post_meta( $order_id, 'my_custom_meta_key', 'my data' );
} , 10, 2);
-
$order_id
is the id of the order, which is stored as a custom post type -
$posted
is all the data from$_POST
$ order_id是订单的ID,存储为自定义帖子类型
$ posted是来自$ _POST的所有数据