Home / Admin / woo orders page Whatsapp DM from mac app
Duplicate Snippet

Embed Snippet on Your Site

woo orders page Whatsapp DM from mac app

Code Preview
php
<?php
// Modify the columns and ensure WhatsApp column appears last
function add_whatsapp_column($columns) {
    // Remove WhatsApp column if it already exists
    if (isset($columns['whatsapp'])) {
        unset($columns['whatsapp']);
    }
    // Add WhatsApp column after Courier Address column
    if (isset($columns['courier_address'])) {
        $new_columns = [];
        foreach ($columns as $key => $value) {
            $new_columns[$key] = $value;
        }
        $new_columns['whatsapp'] = 'WhatsApp';
        return $new_columns;
    }
    // If Courier Address column is not found, simply add WhatsApp at the end
    $columns['whatsapp'] = 'WhatsApp';
    return $columns;
}
add_filter('manage_edit-shop_order_columns', 'add_whatsapp_column', 20);
// Display the WhatsApp button in the new column
function display_whatsapp_button($column) {
    global $post, $global_shipping_carriers;
    if ($column == 'whatsapp') {
        // Get the customer's phone number from the order
        $order = wc_get_order($post->ID);
        $phone_number = $order->get_billing_phone();
        $user_id = $order->get_customer_id();
        $first_name = $user_id ? get_user_meta($user_id, 'first_name', true) : '';
        // Check if the phone number is valid
        if (!empty($phone_number)) {
            // Clean up the phone number and pick only the last 10 digits
            $phone_number = substr(preg_replace('/[^0-9]/', '', $phone_number), -10);
            $whatsapp_base_link = "whatsapp://send?phone=91{$phone_number}";
            // Get the order status
            $order_status = $order->get_status();
            $order_id = $order->get_order_number();
            $tracking_number = get_post_meta($post->ID, '_tracking_number', true);
            $shipping_carrier = get_post_meta($post->ID, '_shipping_carrier', true);
            $tracking_link = isset($shipping_carrier) && isset($global_shipping_carriers[$shipping_carrier]['tracking_url']) ? $global_shipping_carriers[$shipping_carrier]['tracking_url'] : '';
            switch ($order_status) {
                case 'processing':
                    $message = "வணக்கம் {$first_name},%0A
                    இளவனம் இணையதளத்தில் நீங்கள் செய்த ஆர்டர் உறுதி செய்யப்பட்டது. %0A
                    *ORDER ID* : {$order_id}";
                    break;
                case 'misha-packed':
                    $message = "Vanakkam {$first_name},%0A
                    Your order has been packed.%0A
                    Thank you for shopping with us!%0A%0A
                    *ORDER ID* : {$order_id}%0A
                    *Tracking Number* : {$tracking_number}%0A
                    *Tracking Link* : {$tracking_link}";
                    break;
                case 'misha-shipping':
                    $message = 'Your order is shipping. Please contact us for further details.';
                    break;
                default:
                    $message = 'Your order status: ' . $order_status;
                    break;
            }
            // Construct the WhatsApp link with the message parameter
            $whatsapp_link = $whatsapp_base_link . '&text=' . $message;
            // Output the WhatsApp button
            echo '<a aria-label="Chat on WhatsApp" href="' . $whatsapp_link . '" class="button button-primary" target="_blank" rel="noopener">WhatsApp</a>';
        } else {
            echo 'Phone number not available.';
        }
    }
}
add_action('manage_shop_order_posts_custom_column', 'display_whatsapp_button');
// Make WhatsApp column sortable
function make_whatsapp_column_sortable($columns) {
    $columns['whatsapp'] = 'whatsapp';
    return $columns;
}
add_filter('manage_edit-shop_order_sortable_columns', 'make_whatsapp_column_sortable');
// Apply CSS to set minimum column width for WhatsApp column
function set_whatsapp_column_width() {
    echo '<style>
        .column-whatsapp { width: 80px !important; min-width: 80px !important; text-align: center; }
    </style>';
}
add_action('admin_head', 'set_whatsapp_column_width');

Comments

Add a Comment