【问题标题】:Save Order item custom field in Woocommerce Admin order pages在 Woocommerce 管理订单页面中保存订单项目自定义字段
【发布时间】:2019-06-21 11:11:45
【问题描述】:

我在后台订单中为每条产品线添加了自定义字段:
(来源:com-pac.ovh)

我的代码:


add_action( 'woocommerce_before_order_itemmeta', 'cfwc_create_custom_field' );
function cfwc_create_custom_field() {

    $args = array(
        'id' => 'custom_text_field_title',
        'label' => __( 'Custom Text Field Title', 'cfwc' ),
        'class' => 'cfwc-custom-field',
        'desc_tip' => true,
        'description' => __( 'Enter the title of your custom text field.', 'ctwc' ),
    );

    woocommerce_wp_text_input( $args );
}

我的问题是我不知道如何保存这些字段。

有什么帮助吗?

【问题讨论】:

  • 在您的示例下方,在文档中
  • 我已经尝试过了,但它不起作用...我的值归零...

标签: php wordpress woocommerce custom-fields orders


【解决方案1】:

2021 年更新 (使用 CRUD 对象方法)

要添加和保存自定义字段以在管理订单编辑页面中订购“订单项”,您将使用以下内容:

// Add a custom field
add_action( 'woocommerce_before_order_itemmeta', 'add_order_item_custom_field', 10, 2 );
function add_order_item_custom_field( $item_id, $item ) {
    // Targeting line items type only
    if( $item->get_type() !== 'line_item' ) return;

    woocommerce_wp_text_input( array(
        'id'            => 'cfield_oitem_'.$item_id,
        'label'         => __( 'Custom Text Field Title', 'cfwc' ),
        'description'   => __( 'Enter the title of your custom text field.', 'ctwc' ),
        'desc_tip'      => true,
        'class'         => 'woocommerce',
        'value'         => wc_get_order_item_meta( $item_id, '_custom_field' ),
    ) );
}

// Save the custom field value
add_action('save_post', 'save_order_item_custom_field_value', 10000, 2 );
function save_order_item_custom_field_value( $post_id, $post ){
    if ( 'shop_order' !== $post->post_type )
        return $post_id;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    if ( ! current_user_can( 'edit_shop_order', $post_id ) )
        return $post_id;

    $order = wc_get_order( $post_id );

    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {
        if( isset( $_POST['cfield_oitem_'.$item_id] ) ) {
            $item->update_meta_data( '_custom_field', sanitize_text_field( $_POST['cfield_oitem_'.$item_id] ) );
            $item->save();
        }
    }
    $order->save();
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


补充

可选择将新的元键/值隐藏在后端

add_filter( 'woocommerce_hidden_order_itemmeta', 'additional_hidden_order_itemmeta', 10, 1 );
function additional_hidden_order_itemmeta( $args ) {
    $args[] = '_custom_field';
    return $args;
}

可选地更改显示的元键标签

add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
    // Change displayed label for specific order item meta key
    if( is_admin() && $item->get_type() === 'line_item' && $meta->key === '_custom_field' ) {
        $display_key = __("Some label", "woocommerce" );
    }
    return $display_key;
}

【讨论】:

  • 完美!有用。非常感谢您的回答。
  • @LoicTheAztec 而不是在我们保存整个订单时触发的“save_post_shop_order”,您知道当用户尝试在订单项视图中编辑和保存订单项时触发的挂钩名称吗?保存按钮出现在所有订单项的底部。
  • @LoicTheAztec 嗨,我遵循你的代码,但我得到了一些非常奇怪的东西。在“save_order_item_custom_field_value”钩子中,我更新了我的项目元并使用“wc_get_order_item_meta”检查更新的值。它似乎已更新为新值,但是当我访问订单页面时,该值恢复为以前的值。知道为什么吗?
  • @HWSiew我已经在上一个 WooCommerce 版本上测试了代码,它仍然可以完美运行……所以还有其他问题正在制造麻烦(我猜不出它是什么)。如果你喜欢/想要你也可以请upvote 回答,谢谢。
  • @LoicTheAztec,我想知道如果我们在第一次保存后更新文本字段,你是否可以测试它是否有效。我使用默认主题启动了一个完整的新站点,并且仅使用 function.php 中的代码粘贴激活了 woocommerce。它仅适用于第一次保存,但我无法从管理仪表板更新后续订单更新中的值。本地和远程主机的行为是一致的。
猜你喜欢
  • 2019-04-14
  • 2017-12-13
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 2016-07-28
  • 2020-08-12
  • 2021-10-19
  • 1970-01-01
相关资源
最近更新 更多