Kristian
Kristian

Reputation: 41

How to add "Local Pickup Plus pickup date" to a new column in WooCommerce admin order list

I am trying to add a column in WooCommerce admin order list where the value from the Pickup Date from the plugin Local Pickup Plus should be displayed. This gives a better overview of pickup dates.

Bonus: If possible, it would be great if the column could be clicked so that it sorts all pickup dates (first pickup date shows first etc.)

So far I got the following code which successfully added a column, now the column should be populated with pickup date data.

add_filter( 'manage_edit-shop_order_columns', 'bbloomer_add_new_order_admin_list_column' );
function bbloomer_add_new_order_admin_list_column( $columns ) {
    $columns['pickup_date'] = 'Afhentningsdato';
    return $columns;
}
 
add_action( 'manage_shop_order_posts_custom_column', 'bbloomer_add_new_order_admin_list_column_content' );   
function bbloomer_add_new_order_admin_list_column_content( $column ) {
    global $post;

    if ( 'pickup_date' === $column ) {
        // Add pickup date for each order here
    }
}

Upvotes: 1

Views: 826

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

This should suffice, explanation via comment tags added to the code

  • Appointment start & end values are set to timestamps so date function is used to format to a local time/date
// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
    // Add new column
    $columns['pickup_date'] = 'Afhentningsdato';

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Populate the Column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
    // Compare
    if ( $column == 'pickup_date' ) {
        // Get order
        $order = wc_get_order( $post_id );
        
        // Loop though order shipping methods
        foreach( $order->get_items('shipping') as $shipping_item ) {
            // Get appoitment start
            $pickup_appointment_start = $shipping_item->get_meta( '_pickup_appointment_start' );
            $starttime = date( 'H:i', $pickup_appointment_start );
            $startdate = date( 'd-m-Y', $pickup_appointment_start );
            
            // Get appointment end
            $pickup_appointment_end = $shipping_item->get_meta( '_pickup_appointment_end' );
            $endtime = date( 'H:i', $pickup_appointment_end );
            $enddate = date( 'd-m-Y', $pickup_appointment_end );

            echo '<p>ST = ' . $starttime . '</p>';
            echo '<p>SD = ' . $startdate . '</p>';
            echo '<p>ET = ' . $endtime . '</p>';
            echo '<p>ED = ' . $enddate . '</p>';
        }       
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

Upvotes: 2

Related Questions