Home » Php » php – Display custom checkbox field on woocomerce admin order list

php – Display custom checkbox field on woocomerce admin order list

Posted by: admin February 25, 2020 Leave a comment

Questions:

Following that solutions here i am trying to create a custom filed for each order in Woocomerce, and instead of managing it, at customer level – checkout page, i want to manage it only as admin at admin order list view.
so i have the custom field (checkbox) created

function ERP_Checkbox() {
    echo '<div id="ERP_Checkbox">';

    woocommerce_form_field( 'ERP', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('Integrated on ERP'),
    ),  WC()->checkout->get_value( 'ERP' ) );

    echo '</div>';
}

Also i need to save the value when checked from the admin

function save_order_custom_meta_data( $order, $data ) {
    if ( isset($_POST['ERP']) )
        $order->update_meta_data('ERP', '1' );
}

Then my in order to display it at admin order list

function display_ERP_order_column_data( $column ) {
    global $the_order, $post;

    if( $column  == 'ERP Sent' ) {
        if( $ERP= $the_order->get_meta( 'ERP' ) ) {
            echo __("Yes", "woocommerce");
        } else {
            echo ' - ';
        }
    }
}

I am totally not familiar with woocommerce editing and PHP as well. Definitely i am missing actions to display the results.
Can anyone help me on that?

How to&Answers:

If I understand correctly you want to add a new column to the woocomerce admin order list with a checkbox (see code below)

what exactly should the checkbox do? just on or off?

/**
 * Adds 'ERP' column header to 'Orders' page immediately after 'Total' column.
 */
function add_erp_order_column( $columns ) {
    $new_columns = array();

    foreach ( $columns as $column_name => $column_info ) {

        $new_columns[ $column_name ] = $column_info;

        if ( $column_name === 'order_total' ) {
            $new_columns['erp'] = __( 'ERP SENT', 'woocommerce' );
        }
    }

    return $new_columns;
}
add_filter('manage_edit-shop_order_columns', 'add_erp_order_column', 10, 1 );

/**
 * Display data to 'ERP' column.
 */
function display_enclosed_invoice_order_column_data( $column ) {
    global $the_order, $post;

    if( $column == 'erp' ) {
        woocommerce_form_field( 'erp_checkbox', array(
            'type'      => 'checkbox',
            'required'  => true,
        ));
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'display_enclosed_invoice_order_column_data' );