将自定义产品元数据传递到Woocommerce 3中的订单 [英] Pass custom product meta data to the order in Woocommerce 3

查看:84
本文介绍了将自定义产品元数据传递到Woocommerce 3中的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我试图向产品添加一块自定义元,我希望将其传递给订单。

In Woocommerce, I'm attempting to add a piece of custom meta to my products and I would like to pass it through to orders.

我们有大量的产品,它们要对不同的成本中心负责,因此我需要在产品管理员内部选择一个框,以便我们选择通过价值到订单中,客户不必查看,但需要管理员在订单中以及每个月的订单出口中都可以查看以进行会计处理。

We have a substantial amount of products and they are accountable to different cost centers so I need a select box inside the product admin that we can choice the cost centers that passes a value into an order this does not need to be viewed by the customer but needs to be viewable by admin in the orders and also in the order exports each month for accounting.

这就是我到目前为止的内容,它将在产品编辑页面(admin)中显示选择框:

This is what I have so far, this will display the select box in product edit pages (admin):

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

function woo_add_custom_general_fields() {

  global $woocommerce, $post;

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

    woocommerce_wp_select( 
    array( 
    'id'      => '_select', 
    'label'   => __( 'Cost Centre', 'woocommerce' ), 
    'options' => array(
        'one'   => __( 'MFEG', 'woocommerce' ),
        'two'   => __( 'YDIT', 'woocommerce' ),
        )
    )
);

  echo '</div>';

}

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields_save( $post_id ){


    // Select
    $woocommerce_select = $_POST['_select'];
    if( !empty( $woocommerce_select ) )
        update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );

}

但它不会将值传递给订单。

But it is not passing the value in to an order.

如何将此自定义字段值传递给订单?

How can I pass this custom field value to the order?

推荐答案

我再次回顾了您的代码。以下内容将保存您的产品自定义字段费用中心作为隐藏的订单项元数据,仅在每个项目的管理订单编辑页面中可见:

I have revisited a bit your code. The following will save your product custom field "Cost centre" as hidden order item meta data, visible only in Admin Order edit pages on each item:

// 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)
    }
}

代码已输入您的活动子主题(或活动主题)的functions.php文件。经过测试并可以使用。

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


导出: (在StackOverFlow中,规则为一个问题的一个问题)

WordPress / Woocommerce基本订单导出不允许导出订单项

The WordPress/Woocommerce basic order export don't allow to export order items

您将需要使用第三方插件,具体取决于选择的插件,您将必须根据插件的可能性为导出添加订单项自定义字段 _cost_centre

You will need to use a third party plugin and depending on the chosen plugin, you will have to add your order item custom field _cost_centre for your export based on the plugin possibilities.

这篇关于将自定义产品元数据传递到Woocommerce 3中的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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