Wordpress 如何填充Woocommerce手动订单的元字段

Wordpress 如何填充Woocommerce手动订单的元字段,wordpress,woocommerce,hook-woocommerce,Wordpress,Woocommerce,Hook Woocommerce,我为我的订单定义了两个自定义字段。我曾经创建过这两个字段 当我手动创建订单(/wp admin/post new.php?post_type=shop_order)时,我可以看到这些字段,并且可以成功地添加数据并保存订单 但是,我想在代码中自动填充其中一个字段。我正在尝试使用以下代码查找该自定义字段的元键,以便使用update\u post\u meta()填充它,但在运行它时,这不会给我任何信息: add_action('woocommerce_process_shop_order_meta'

我为我的订单定义了两个自定义字段。我曾经创建过这两个字段

当我手动创建订单(/wp admin/post new.php?post_type=shop_order)时,我可以看到这些字段,并且可以成功地添加数据并保存订单

但是,我想在代码中自动填充其中一个字段。我正在尝试使用以下代码查找该自定义字段的元键,以便使用
update\u post\u meta()
填充它,但在运行它时,这不会给我任何信息:

add_action('woocommerce_process_shop_order_meta', 'process_offline_order', 10, 2);
function process_offline_order ($post_id, $post) {
    echo '<pre>';
    print_r(get_post_meta($post_id));
    die();
}
add_action('woocmerce_process_shop_order_meta'、'process_offline_order',10,2);
函数处理\u脱机\u订单($post\u id,$post){
回声';
打印(获取发布元数据($post\u id));
模具();
}
该扩展的文档告诉我,我可以使用类似于
get_post_meta($post_id,'.\u wc_acof_2')
的东西,其中2是该自定义字段的id,我也尝试过,但没有成功。它没有归还任何东西

下面是我的配置屏幕的屏幕截图:


如果我使用的钩子不正确,你知道我如何访问/填充这些字段,以及使用什么钩子吗?

你应该尝试使用“save_post”操作钩子,方法如下:

//提交时保存(更新)或执行操作
添加操作('save_post','update_order_custom_field_value');
函数更新\顺序\自定义\字段\值($post\ id){
//仅适用于商店订单
如果('shop\u order'!=$\u POST['POST\u type'])
返回$post_id;
//检查是否为自动保存
if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE)
返回$post_id;
//检查用户权限(针对“店铺经理”和“管理员”用户角色)
如果(!当前用户可以('edit_shop_order',$post_id)&&!当前用户可以('edit_shop_order',$post_id))
返回$post_id;
//更新自定义字段数据
如果(isset($_POST['wc-admin-custom-order-fields'][2])){
//新价值
$value='绿色';
//或者获取一个订单元数据值(这里用正确的metakey slug替换“meta_key”)
//$value=get_post_meta($post_id,'meta_key',true);//(字符串使用“true”,数组使用“false”)
//替换和更新值
更新发布元($post\u id,''u wc\u acof\u 2',$value);
}
}
//按订单编辑页面测试输出(账单地址下方):
添加操作('woocommerce\u admin\u order\u data\u在账单地址之后,'display\u order\u custom\u field\u value');
函数显示\顺序\自定义\字段\值($order){
foreach(get_选项('wc_admin_custom_order_fields')为$id=>$field){
$value=get_post_meta($order->get_id(),''u wc\u acof'.$id,true);
//在下面定义要显示的自定义字段ID
如果($id==2&&!空($value)){
回显“”.$field['label']”:“.$value.”

”; } } }

代码会出现在活动子主题(或主题)的function.php文件中,或者任何插件文件中。

您能分享这些自定义字段的配置屏幕截图吗?Andrew,我刚刚添加了一个截图。谢谢。你的代码看起来很有前途,但不幸的是,它不适合我。首先,它没有像代码中那样将自定义字段值更新为“绿色”,其次,当我转储
$\u POST
请求时,我没有看到请求中出现
\u wc\u acof\u 2
。你能添加一个转储POST数组的样子吗?自定义帖子字段的字段名可能不正确。谢谢,我将
if(isset($\u post[''''u wc\u acof_2']){
替换为
if(isset($\u post['wc-admin-custom-order-fields'][2]){
并且它现在正在工作。非常好,很高兴能提供帮助。
// Saving (Updating) or doing an action when submitting
add_action( 'save_post', 'update_order_custom_field_value' );
function update_order_custom_field_value( $post_id ){

    // Only for shop order
    if ( 'shop_order' != $_POST[ 'post_type' ] )
        return $post_id;

    // Checking that is not an autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    // Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
    if ( ! current_user_can( 'edit_shop_order', $post_id ) && ! current_user_can( 'edit_shop_orders', $post_id ) )
        return $post_id;

    // Updating custom field data
    if( isset( $_POST['wc-admin-custom-order-fields'][2] ) ) {
        // The new value
        $value = 'Green';

        // OR Get an Order meta data value ( HERE REPLACE "meta_key" by the correct metakey slug)
        // $value = get_post_meta( $post_id, 'meta_key', true ); // (use "true" for a string or "false" for an array)

        // Replacing and updating the value
        update_post_meta( $post_id, '_wc_acof_2', $value );
    }
}


// Testing output in order edit pages (below billing address):
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_order_custom_field_value' );
function display_order_custom_field_value( $order ){
    foreach( get_option( 'wc_admin_custom_order_fields' ) as $id => $field ){
        $value = get_post_meta( $order->get_id(), '_wc_acof_'.$id, true );
        // Define Below the custom field ID to be displayed
        if( $id == 2 && ! empty( $value ) ){
            echo '<p><strong>' . $field['label'] . ':</strong> ' . $value . '</p>';
        }
    }
}