Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Woocommerce 如何通过删除“";wc“uquot;前缀_Woocommerce - Fatal编程技术网

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后端中看到的订单号是
    post\u id
    Wordpress数据库的
    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
表中的order
post\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 );

}