Home / Admin / woo orders page courier address
Duplicate Snippet

Embed Snippet on Your Site

woo orders page courier address

Code Preview
php
<?php
// Add the new column to the orders page
add_filter('manage_edit-shop_order_columns', 'display_courier_address_column');
// Display data in the new column
add_action('manage_shop_order_posts_custom_column', 'display_courier_address_data', 10, 2);
function display_courier_address_column($columns) {
    // Add the new column with a proper name
    $columns['courier_address'] = 'Courier Address';
    return $columns;
}
function display_courier_address_data($column, $post_id) {
    if ($column == 'courier_address') {
        // Retrieve order details
        $order = wc_get_order($post_id);
        // Fetch customer details safely
        $customer_phone = $order->get_billing_phone();
        $customer_address = remove_tamil_nadu($order->get_formatted_billing_address());
        // Fetch ordered items with product_shortform
        $ordered_items = [];
        foreach ($order->get_items() as $item_id => $item) {
            $product_shortform = get_post_meta($item->get_product_id(), 'product_shortform', true);
            $display_name = !empty($product_shortform) ? $product_shortform : 'NA';
            $ordered_items[] = $item->get_quantity() . ' ' . $display_name;
        }
        // Convert ordered items array to a string
        $ordered_items_str = implode(', ', $ordered_items);
        // Output formatted data (Customer Name Removed)
        echo '<strong>To</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $ordered_items_str . '<br>';
        echo $customer_address . '<br>';
        echo 'Ph - ' . (!empty($customer_phone) ? $customer_phone : 'NA') . '<br>';
    }
}
// Custom function to remove "Tamil Nadu" and ensure only one <br> remains
function remove_tamil_nadu($address) {
    // Remove "Tamil Nadu" and any adjacent <br> tags (before or after)
    $address = preg_replace('/(<br>\s*)*Tamil Nadu(\s*<br>)*/i', '', $address);
    // Remove multiple consecutive <br> tags, keeping only one
    $address = preg_replace('/(<br>\s*){2,}/', '<br>', $address);
    // Trim any leading or trailing <br> tags
    $address = trim($address, "<br> \t\n\r\0\x0B");
    return $address;
}

Comments

Add a Comment