Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Amanda Barker

Make custom fields filterable

Hi there,

I’ve used code snippet below to create some custom fields in Woo Orders. I have then used Admin Columns to make these fields appear in the Order Admin page. I want the ‘order_sorting’ field to be filterable, and have checked the Admin Columns settings, however no content appears in the filter box. What can I do to create filterable custom column fields?

Many thanks


// Add columns to admin orders table.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
    $columns['shipping_service'] = __( 'Shipping Service', 'woocommerce' );
    $columns['tracking_number'] = __( 'Tracking number', 'woocommerce' );
    $columns['shipping_date'] = __( 'Shipping date', 'woocommerce' );
    $columns['order_sorting'] = __( 'Pre-orders', 'woocommerce' );
    return $columns;
}

// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
    switch ( $column ) {

        case 'shipping_service' :
            echo esc_html( get_post_meta( $post_id, 'shipping_service', true ) );
            break;

        case 'tracking_number' :
            echo esc_html( get_post_meta( $post_id, 'tracking_number', true ) );
            break;

        case 'shipping_date' :
            echo esc_html( get_post_meta( $post_id, 'shipping_date', true ) );
            break;
        case 'order_sorting' :
            echo esc_html( get_post_meta( $post_id, 'order_sorting', true ) );
            break;

    }
}

// Add a metabox.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {

    add_meta_box(
        'custom_meta_box',
        __( 'Tracking information', 'woocommerce' ),
        'shop_order_content_callback',
        'shop_order'
    );

}

// For displaying metabox content
function shop_order_content_callback( $post ) {

    $shipping_service = get_post_meta( $post->ID, 'shipping_service', true );
    echo '<p>' . __( 'Shipping Service', 'woocommerce' ) . '<br>
    <input type="text" style="width:100%" id="shipping_service" name="shipping_service" value="' . esc_attr( $shipping_service ) . '"></p>';

    $tracking_number = get_post_meta( $post->ID, 'tracking_number', true );

    echo '<p>' . __( 'Tracking Number', 'woocommerce' ) . '<br>
    <input type="text" style="width:100%" id="tracking_number" name="tracking_number" value="' . esc_attr( $tracking_number ) . '"></p>';

    $shipping_date = get_post_meta( $post->ID, 'shipping_date', true );

    echo '<p>' . __( 'Shipping Date', 'woocommerce' ) . '<br>
    <input type="text" style="width:100%" id="shipping_date" name="shipping_date" value="' . esc_attr( $shipping_date ) . '"></p>';

    $shipping_date = get_post_meta( $post->ID, 'shipping_date', true );

    echo '<p>' . __( 'order_sorting', 'woocommerce' ) . '<br>
    <input type="text" style="width:100%" id="order_sorting" name="order_sorting" value="' . esc_attr( $shipping_date ) . '"></p>';

}

// For saving the metabox data.
add_action( 'save_post_shop_order', 'save_shop_order_meta_box_data' );
function save_shop_order_meta_box_data( $post_id ) {

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
        return;
    }

    // Make sure that 'shipping_service' is set.
    if ( isset( $_POST['shipping_service'] ) ) {

        // Update the meta field in the database.
        update_post_meta( $post_id, 'shipping_service', sanitize_textarea_field( $_POST['shipping_service'] ) );
    }

    // Make sure that 'tracking_number' it is set.
    if ( isset( $_POST['tracking_number'] ) ) {

        // Update the meta field in the database.
        update_post_meta( $post_id, 'tracking_number', sanitize_text_field( $_POST['tracking_number'] ) );
    }

    // Make sure that 'shipping_date' is set.
    if ( isset( $_POST['shipping_date'] ) ) {

        // Update the meta field in the database.
        update_post_meta( $post_id, 'shipping_date', sanitize_text_field( $_POST['shipping_date'] ) );
    }

    // Make sure that 'order_sorting' is set.
    if ( isset( $_POST['order_sorting'] ) ) {

        // Update the meta field in the database.
        update_post_meta( $post_id, 'order_sorting', sanitize_text_field( $_POST['order_sorting'] ) );
    }

}

3 years, 2 months ago
Stefan van den Dungen Gronovius
Developer

I see that you wrote your own columns. The pro features for our plugin only work for default WordPress columns, our custom columns, and for some plugins where we wrote a specific integration. I see that your custom column are simple columns that just display a simple custom field for the WooCommerce orders.

In that case, you can replace your custom columns with our Custom field column. It basically does the same, only with our column you have all the pro features available.

3 years, 2 months ago
Amanda Barker

Hi Stefan, thanks for the reply. I had a look this morning and it’s working! The fields are created with the code above but the columns are created with Admin Columns custom field – not sure why it wasn’t working yesterday – does the column need to contain a certain amount of data before the filter works? I think I just filled the field for one order, refreshed the page and it wasn’t coming up in the filter. My colleague has filled the field with a few more orders and now it seems to be there!

Thanks again

3 years, 2 months ago

You must be logged in to reply to this topic.