将自定义产品元数据传递给 Woocommerce 3 中的订单
Pass custom product meta data to the order in Woocommerce 3
在 Woocommerce 中,我正在尝试向我的产品添加一段自定义元数据,我想将其传递给订单。
我们有大量的产品,它们对不同的成本中心负责,所以我需要在产品管理中有一个 select 框,我们可以选择将值传递到订单中的成本中心。不需要被客户查看,但需要管理员在订单中查看,并且在每个月的订单导出中进行核算。
这是我目前所拥有的,这将在产品编辑页面中显示 select 框 (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 ) );
}
但它没有将值传递给订单。
如何将此自定义字段值传递给订单?
我重新审视了您的代码。以下将把您的产品自定义字段“成本中心”保存为隐藏的订单项目元数据,仅在每个项目的管理订单编辑页面中可见:
// 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 文件。已测试并有效。
Export: (In Whosebug, the rule is one question for one answer).
The WordPress/Woocommerce basic order export don't allow to export order items
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 中,我正在尝试向我的产品添加一段自定义元数据,我想将其传递给订单。
我们有大量的产品,它们对不同的成本中心负责,所以我需要在产品管理中有一个 select 框,我们可以选择将值传递到订单中的成本中心。不需要被客户查看,但需要管理员在订单中查看,并且在每个月的订单导出中进行核算。
这是我目前所拥有的,这将在产品编辑页面中显示 select 框 (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 ) );
}
但它没有将值传递给订单。
如何将此自定义字段值传递给订单?
我重新审视了您的代码。以下将把您的产品自定义字段“成本中心”保存为隐藏的订单项目元数据,仅在每个项目的管理订单编辑页面中可见:
// 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 文件。已测试并有效。
Export: (In Whosebug, the rule is one question for one answer).
The WordPress/Woocommerce basic order export don't allow to export order items
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.