0

I run a WooCommerce store and work with lots of delivery companies. To keep things organized, I added a custom post type where I can add new delivery companies. The meta box in the order details shows me the companies I created. Everything's good, but I can't seem to get the delivery company info to show up in a new column in the orders list.

here is the code used to create the custom meta box.

added meta box with a drop-down list to choose in order details which company this order   
        add_action( 'add_meta_boxes', 'add_delivery_company_to_order' );
             function add_delivery_company_to_order() {
                add_meta_box(
                  'delivery_company_to_order',
                 'Delivery Company',
            'display_delivery_company_to_order',
            'shop_order',
            'side',
            'default'
        );
    }
    
    function display_delivery_company_to_order( $post ) {
        $delivery_companies = get_posts( array(
            'post_type' => 'delivery_company',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order' => 'ASC',
        ) );
        $selected_delivery_company = get_post_meta( $post->ID, 'delivery_company', true );
        echo '<select id="delivery_company" name="delivery_company">';
        echo '<option value="">' . __( 'Select a delivery company', 'woocommerce' ) . '</option>';
        foreach ( $delivery_companies as $delivery_company ) {
            $selected = selected( $selected_delivery_company, $delivery_company->ID, false );
            echo '<option value="' . $delivery_company->ID . '" ' . $selected . '>' . $delivery_company->post_title . '</option>';
        }
        echo '</select>';
    }
    
    add_action( 'save_post', 'save_delivery_company_to_order' );
    function save_delivery_company_to_order( $post_id ) {
        if ( array_key_exists( 'delivery_company', $_POST ) ) {
            update_post_meta(
                $post_id,
                'delivery_company',
                $_POST['delivery_company']
            );
        }
    }

here is the code used to create a new column to show the value

add_filter( 'manage_shop_order_posts_columns', 'add_delivery_company_column' );
function add_delivery_company_column( $columns ) {
    $new_columns = array();
    foreach ( $columns as $column_name => $column_info ) {
        $new_columns[ $column_name ] = $column_info;
        if ( 'order_total' === $column_name ) {
            $new_columns['delivery_company'] = __( 'Delivery Company', 'woocommerce' );
        }
    }
    return $new_columns;
}

add_action( 'manage_shop_order_posts_custom_column', 'display_delivery_company_in_order_list', 2 );
function display_delivery_company_in_order_list( $column ) {
    global $post;
    if ( 'delivery_company' === $column ) {
        $delivery_company_id = get_post_meta( $post->ID, 'delivery_company', true );
        if ( $delivery_company_id ) {
            $delivery_company = get_post( $delivery_company_id );
            echo $delivery_company->post_title;
        } else {
            echo '-';
        }
    }
}

Any ideas? thanks and sorry for my English

1 Answer 1

0

Solved it here is the answer

add_action( 'manage_shop_order_posts_custom_column', 'delivery_company_order_column_content', 10, 2 );
function delivery_company_order_column_content( $column, $post_id ) {
    if ( $column === 'delivery_company_order' ) {
        $order = wc_get_order( $post_id );
        $delivery_company = get_post_meta( $order->get_id(), 'delivery_company', true );
        $delivery_company_name = get_the_title( $delivery_company );
        echo $delivery_company_name;
    }
}

add_filter('manage_edit-shop_order_columns', 'delivery_company_order_column');
function delivery_company_order_column($columns)
{
    $columns['delivery_company_order'] = 'Delivery Company';
    return $columns;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.