如何填充Woocommerce手动订单的元字段 [英] How can I populate meta fields of a Woocommerce manual order

查看:56
本文介绍了如何填充Woocommerce手动订单的元字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Woocommerce订单定义了两个自定义字段.我使用了 WooCommerce管理员自定义订单字段扩展来创建这些两个字段.

I have defined two custom fields for my Woocommerce orders. I used WooCommerce Admin Custom Order Fields extension to create these two fields.

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

When I manually create an order (/wp-admin/post-new.php?post_type=shop_order), I can see those fields and I can successfully add data and save the order.

但是,我想自动在代码中填充其中一个字段.我正在尝试使用以下代码来查找该自定义字段的meta键,以便可以使用update_post_meta()进行填充,但这在运行时不会给我任何帮助:

I would, however, like to automatically populate one of those fields in the code. I'm trying to use the following code to do find out the meta key to that custom field so I can use update_post_meta() to populate it, but this does not give me anything when I run it:

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();
}

该扩展名的文档告诉我,我可以使用类似get_post_meta($post_id, '_wc_acof_2')之类的东西,其中2是该自定义字段的ID,我也尝试过运气.它没有返回任何东西.

The documentation to that extension tells me that I can use something like get_post_meta($post_id, '_wc_acof_2') with 2 being the ID of that custom field and I tried that too with no luck. It's not returning anything.

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

Below is an screen shot of my configuration screen:

有什么主意,我如何访问/填充这些字段,以及如果我使用的那个字段不正确,该使用哪个钩子?

Any idea how I can access/populate those fields and what hook to use if the one I'm using is not correct?

推荐答案

您应尝试通过以下方式使用"save_post"操作钩子:

You should try to use instead "save_post" action hook this way:

// 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>';
        }
    }
}

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

这篇关于如何填充Woocommerce手动订单的元字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆