将产品自定义字段另存为WooCommerce管理员手动订单的自定义订单项元数据 [英] Save product custom-field as custom order item metadata for WooCommerce admin manual orders

查看:94
本文介绍了将产品自定义字段另存为WooCommerce管理员手动订单的自定义订单项元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 在Woocommerce 3的应答代码中将自定义产品元数据传递给订单,当从后端手动创建订单时从后端手动添加产品时,是否可以保存和显示自定义元数据?

Using Pass custom product meta data to the order in Woocommerce 3 answer code, Is it possible to save and display custom metadata when the product is added manually from backend when creating orders manually from the backend?

这是我的代码(稍作更改):

// Admin products: Display custom Field
add_action( 'woocommerce_product_options_general_product_data', 'product_options_general_product_data_add_field' );
function product_options_general_product_data_add_field() {
    global $post;

    echo '<div class="options_group">';

    woocommerce_wp_select( array(
        'id'      => '_cost_centre',
        'label'   => __( 'Cost Centre', 'woocommerce' ),
        'options' => array(
            'MFEG'   => __( 'MFEG', 'woocommerce' ), // Default displayed option value
            'YDIT'   => __( 'YDIT', 'woocommerce' ),
        )
    ) );

    echo '</div>';
}

// Admin products: Save custom Field
add_action( 'woocommerce_process_product_meta', 'product_options_general_product_data_save_field' );
function product_options_general_product_data_save_field( $post_id ){
    if( isset( $_POST['_cost_centre'] ) )
        update_post_meta( $post_id, '_cost_centre', esc_attr( $_POST['_cost_centre'] ) );
}

// Order items: Save product "Cost centre" as hidden order item meta data
add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
    if ( $cost_centre = $values['data']->get_meta('_cost_centre') ) {
        $item->update_meta_data( '_cost_centre', $cost_centre ); // Save as order item (visble on admin only)
    }
}

这很好当客户从前端创建订单时。但是当管理员从后端手动创建订单并添加产品时,自定义元数据不可见。

This works fine when the order is created by the client from the frontend. But when admin creates an order manually from the backend and adds the product, the custom metadata is not visible.

如何解决手动创建的订单的此问题,从而允许将产品自定义字段添加为自定义订单商品数据?

How to solve this problem for orders created manually, allowing to add product custom field as custom order item data?

推荐答案

更新3

对于手动后端订单,您可以尝试使用 woocommerce_before_save_order_item 专用操作挂钩,如下所示(基于您的问题代码的代码)

For manual backend orders, you can try to use woocommerce_before_save_order_item dedicated action hook as follow (code based on your question code):

add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
    $cost_centre = $item->get_meta('_cost_centre');
    // If custom meta data is not saved as order item
    if ( empty($cost_centre) ) {
        // Get custom meta data from the product
        $cost_centre = get_post_meta( $item->get_product_id(), '_cost_centre', true );
        $cost_centre = empty($cost_centre) ? 'MFEG' : $cost_centre;
        
        // Save it as custom order item (if defined)
        $item->update_meta_data( '_cost_centre', $cost_centre );
    }
}

代码在您的活动子主题(或活动主题)的functions.php文件中主题)。 经过测试且有效

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

添加:使订单项自定义元数据对客户可见

如果希望此订单项元数据在客户订单和电子邮件通知上显示 ,则将替换中的订单项元键从'_cost_centre''Cost center'如下:

If you want this order item meta data to be visible on customer orders and email notifications, you will replace the order item meta key from '_cost_centre' to 'Cost centre' as follow:

add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
    $cost_centre = $item->get_meta('_cost_centre');
    // If custom meta data is not saved as order item
    if ( empty($cost_centre) ) {
        // Get custom meta data from the product
        $cost_centre = get_post_meta( $item->get_product_id(), 'Cost centre', true );
        $cost_centre = empty($cost_centre) ? 'MFEG' : $cost_centre;
        
        // Save it as custom order item (if defined)
        $item->update_meta_data( 'Cost centre', $cost_centre );
    }
}

这一次它将在客户订单和电子邮件上显示。

This time it will be visible on customer orders and emails.

您还需要将问题代码的最后一个功能更改为:

You will need also to change your last function on your question code to:

// Order items: Save product "Cost centre" as visible order item meta data
add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4);
function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) {
    if ( $cost_centre = $values['data']->get_meta('_cost_centre') ) {
        $item->update_meta_data( 'Cost centre', $cost_centre ); // Save as order item (visible everywhere)
    }
}



注意::订单项自定义元键以下划线开头时,它是隐藏的。

Note: When the order item custom meta key starts with an underscore, it's hidden.

这篇关于将产品自定义字段另存为WooCommerce管理员手动订单的自定义订单项元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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