Woocommerce 如何通过删除“";wc“uquot;前缀
我想通过从订单键中删除Woocommerce 如何通过删除“";wc“uquot;前缀,woocommerce,Woocommerce,我想通过从订单键中删除wc\uu来操作订单键。 问题是,无论我做什么,wc\uu都不会被删除 有什么方法可以做到这一点吗? add_filter( 'woocommerce_generate_order_key', 'woocommerce_generate_custom_order_key', 10, 1 ); function woocommerce_generate_custom_order_key($order_key){ $order_key = str_replace( 'wc_',
wc\uu
来操作订单键。
问题是,无论我做什么,wc\uu
都不会被删除
有什么方法可以做到这一点吗?
add_filter( 'woocommerce_generate_order_key', 'woocommerce_generate_custom_order_key', 10, 1 );
function woocommerce_generate_custom_order_key($order_key){
$order_key = str_replace( 'wc_', '', $order_key );
return $order_key;
}
\u order\u key
(order key)和post\u id
(order id)字段不同
- 您在WooCommerce后端中看到的订单号是
Wordpress数据库的post\u id
表的字段wp_posts
- 顺序键是指Wordpress数据库的
表的wp\u Posteta
\u顺序键(meta\u键)字段
woocommerce\u generate\u order\u key
过滤器已记录在案,如您所见,您无法使用它从订单的\u order\u key
字段中删除wc\u前缀。以下是报告的摘录:
如果要操作WOOCERDER键,必须使用WC\u order类的set\u order\u key()
方法。您可以找到文档
您还需要更新wp\u posts
表中的orderpost\u password
字段
因此,例如,如果您想为每个新订单设置自定义订单键(删除wc\uuu前缀),您可以使用以下功能:
// removes the "wc_" prefix from the WooCommerce order key field for each new order
add_action( 'woocommerce_new_order', 'set_custom_order_key', 99, 2 );
function set_custom_order_key( $order_id, $order ) {
// get the order object
$order = wc_get_order( $order_id );
// gets the current order key
$order_key = $order->get_order_key();
// remove the "wc_" prefix
$new_order_key = str_replace( 'wc_', '', $order_key );
// updates the "_order_key" field of the "wp_postmeta" table
$order->set_order_key( $new_order_key );
$order->save();
// updates the "post_password" field of the "wp_posts" table
$post = get_post( $order_id );
$post->post_password = $new_order_key;
wp_update_post( $post );
}
该代码已经过测试,可以正常工作。将其添加到活动主题的functions.php中
如果要操作WooCommerce订单id必须使用WooCommerce\u order\u number
过滤器。可以找到文档
您可以在这里找到一些示例:
// removes the "wc_" prefix from the WooCommerce order key field for each new order
add_action( 'woocommerce_new_order', 'set_custom_order_key', 99, 2 );
function set_custom_order_key( $order_id, $order ) {
// get the order object
$order = wc_get_order( $order_id );
// gets the current order key
$order_key = $order->get_order_key();
// remove the "wc_" prefix
$new_order_key = str_replace( 'wc_', '', $order_key );
// updates the "_order_key" field of the "wp_postmeta" table
$order->set_order_key( $new_order_key );
$order->save();
// updates the "post_password" field of the "wp_posts" table
$post = get_post( $order_id );
$post->post_password = $new_order_key;
wp_update_post( $post );
}