【问题标题】:Pass custom product meta data to the order in Woocommerce 3将自定义产品元数据传递给 Woocommerce 3 中的订单
【发布时间】:2018-09-29 15:34:36
【问题描述】:

在 Woocommerce 中,我正在尝试向我的产品添加一段自定义元数据,并且我想将其传递给订单。

我们有大量产品,它们对不同的成本中心负责,因此我需要产品管理员内的一个选择框,我们可以选择成本中心,将价值传递到不需要查看的订单中客户,但需要由管理员在订单中查看,并且还需要在每个月的订单导出中查看。

这是我目前所拥有的,这将在产品编辑页面中显示选择框(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 ) );

}

但它没有将值传递给订单。

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

【问题讨论】:

    标签: php wordpress woocommerce custom-fields orders


    【解决方案1】:

    我重新访问了您的代码。以下会将您的产品自定义字段“成本中心”保存为隐藏的订单项目元数据,仅在每个项目的管理订单编辑页面中可见:

    // 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 文件中。经过测试并且可以工作。

    导出: (在 StackOverFlow 中,规则是一个问题一个答案

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

    您需要使用第三方插件,并且根据所选插件,您必须根据插件的可能性添加您的订单项目自定义字段_cost_centre 以便导出。 p>

    【讨论】:

      猜你喜欢
      • 2020-07-06
      • 1970-01-01
      • 2019-03-31
      • 2017-09-25
      • 2018-01-03
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多