代码之家  ›  专栏  ›  技术社区  ›  Max

在电子商务管理订单列表自定义列中显示签出字段值

  •  1
  • Max  · 技术社区  · 7 年前

    我被一项任务困住了。我想在woocommerce后端的shop order列中添加一个额外的列。 如果客户在签出时选中复选框字段,则此额外列应显示回显输出。

    add_filter('manage_edit-shop_order_columns', 'invoice_order_overview');
    
    function invoice_order_overview($columns) {
    $new_columns = (is_array($columns)) ? $columns : array();
    unset($new_columns['order_actions']);
    
    //edit this for you column(s)
    //all of your columns will be added before the actions column
    $new_columns['MY_COLUMN_ID_2'] = 'Extra Column';
    //stop editing
    
    $new_columns['order_actions'] = $columns['order_actions'];
    return $new_columns;
    }
    

    现在我想在这个新增的专栏中展示一些东西。签出页面上复选框的功能如下所示。它已经在订单编辑页面显示回显输出。

    // Add custom checkbox field to checkout 
    add_action( 'woocommerce_review_order_before_submit', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field() {
    echo '<div id="my_custom_checkout_field">';
    
    woocommerce_form_field( 'my_field_name', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('Rechnung beilegen? (Sonst nur Lieferschein)'),
    ),  WC()->checkout->get_value( 'my_field_name' ) );
    echo '</div>';
    }
    
    
    // Save the custom checkout field in the order meta, when checkbox has 
    been checked
    add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 1 );
    
    function custom_checkout_field_update_order_meta( $order_id ) {
    
    if ( ! empty( $_POST['my_field_name'] ) )
        update_post_meta( $order_id, 'my_field_name', 
    $_POST['my_field_name'] );
    }
    
    
    // Display the custom field result on the order edit page (backend) 
    when checkbox has been checked
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
    
    function display_custom_field_on_order_edit_pages( $order ){
    $my_field_name = get_post_meta( $order->get_id(), 'my_field_name', 
    true );
    if( $my_field_name == 1 )
        echo '<p style="background: #dba029; padding: 1em !important; 
    color: #fff; font-weight: 700;"><strong>Rechnung beilegen! </strong> 
    </p>';
    }
    

    所以我想应该可以抓住它 $my_field_name 变量并将其放入新的额外列中,如下所示。

    add_action('manage_shop_order_posts_custom_column', 'invoice_order_overview_value', 2);
    
    function invoice_order_overview_value($column) {
    global $post;
    
    if ($column == 'MY_COLUMN_ID_2') {
    
    $my_field_name = get_post_meta( $order->get_id(), 'my_field_name', true );
       if( $my_field_name == 1 )
          echo 'Rechnung beilegen!';
       } 
    }
    

    但这在添加的列中给了我一个“未定义变量”错误。

    如果我把 echo 'Rechnung beilegen!'; 这样地:

    add_action('manage_shop_order_posts_custom_column', 'invoice_order_overview_value', 2);
    
    function invoice_order_overview_value($column) {
    global $post;
    
    if ($column == 'MY_COLUMN_ID_2') {
    
          echo 'Rechnung beilegen!';
       } 
    }
    

    因此,问题是: 如何根据中的选择获取输出 $my_field_name 进入 MY_COLUMN_ID_2

    感谢您的帮助。

    1 回复  |  直到 7 年前
        1
  •  2
  •   LoicTheAztec    7 年前

    以下重新访问的代码将添加自定义列,并显示自定义签出字段“随附发票”值:

    // Add custom checkbox field to checkout
    add_action( 'woocommerce_review_order_before_submit', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field() {
        echo '<div id="my_custom_checkout_field">';
    
        woocommerce_form_field( '_enclosed_invoice', array(
            'type'      => 'checkbox',
            'class'     => array('input-checkbox'),
            'label'     => __('Enclose invoice? (Otherwise only delivery note)'),
        ),  WC()->checkout->get_value( '_enclosed_invoice' ) );
    
        echo '</div>';
    }
    
    // Save the custom checkout field in the order meta, when checkbox has been checked
    add_action( 'woocommerce_checkout_create_order', 'save_order_custom_meta_data', 10, 2 );
    function save_order_custom_meta_data( $order, $data ) {
        if ( isset($_POST['_enclosed_invoice']) )
            $order->update_meta_data('_enclosed_invoice', '1' );
    }
    
    // Display the custom field result on the order edit page (backend) when checkbox has been checked
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
    function display_custom_field_on_order_edit_pages( $order ){
        if( $my_field_name = $order->get_meta( '_enclosed_invoice' ) )
            echo '<p style="background: #dba029; padding: 1em !important; color: #fff; font-weight: 700;"><strong>Enclosed invoice!</strong></p>';
    }
    
    // Add custom column before "Actions" column in admin orders list
    add_filter('manage_edit-shop_order_columns', 'add_enclosed_invoice_order_column', 10, 1 );
    function add_enclosed_invoice_order_column( $columns ) {
        // Woocommerce compatibility since version 3.3
        $actions_key = isset($columns['wc_actions']) ? 'wc_actions' : 'order_actions';
    
        $order_actions = $columns[$actions_key];
    
        unset($columns[$actions_key]);
    
        $columns['enclosed_invoice'] = __("Enc. Invoice", "woocommerce");
    
        $columns[$actions_key] = $order_actions;
    
        return $columns;
    }
    
    // Display data to custom column in admin orders list
    add_action( 'manage_shop_order_posts_custom_column' , 'display_enclosed_invoice_order_column_data' );
    function display_enclosed_invoice_order_column_data( $column ) {
        global $the_order, $post;
    
        if( $column  == 'enclosed_invoice' ) {
            if( $enclosed_invoice = $the_order->get_meta( '_enclosed_invoice' ) ) {
                echo __("Yes", "woocommerce");
            } else {
                echo ' - ';
            }
        }
    }
    

    代码进入活动子主题(活动主题)的function.php文件。测试和工作。

    enter image description here

    'wc_actions' 'order_actions'