skip to Main Content

I am using this woocommerce default code for tracking where customer have to enter both order id and phone number to track the order.

enter image description here

and here is the code for this

// Create a custom tracking shortcode
add_shortcode( 'dvs_order_tracking', 'dvs_tracking_request' );
function dvs_tracking_request() {
    if ( is_null( WC()->cart ) ) {
        return;
    }
    $nonce_value = wc_get_var( $_REQUEST['woocommerce-order-tracking-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) );

    if ( isset( $_REQUEST['orderid'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-order_tracking' ) ) { 
        $order_id    = empty( $_REQUEST['orderid'] ) ? 0 : ltrim( wc_clean( wp_unslash( $_REQUEST['orderid'] ) ), '#' );
        $order_email = empty( $_REQUEST['order_email'] ) ? '' : sanitize_text_field( wp_unslash( $_REQUEST['order_email'] ) );
        $order_phone = empty( $_REQUEST['order_phone'] ) ? '' : sanitize_text_field( wp_unslash( $_REQUEST['order_phone'] ) );   
        $dvs_tracking_phone = get_option( 'dvs_tracking_phone' );

        if ( current_user_can( 'manage_options' ) ) {
            if ( ! $order_id ) {
            wc_print_notice( __( 'Order number is required', 'woocommerce' ), 'error' );
            }
            else {
                $order = wc_get_order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) );
                if ( $order && $order->get_id() ) {
                    do_action( 'woocommerce_track_order', $order->get_id() );
                    wc_get_template(
                        'order/tracking.php',
                        array(
                            'order' => $order,
                        )
                    );
                    return;
                } else {
                    wc_print_notice( __( 'Sorry, order could not be found.', 'woocommerce' ), 'error' );
                }
            }
        }
        else if($dvs_tracking_phone == 1) {
            if ( ! $order_id ) {
                wc_print_notice( __( 'Order number is required', 'woocommerce' ), 'error' );
            } elseif ( ! $order_phone ) {
                wc_print_notice( __( 'Mobile Number is required', 'woocommerce' ), 'error' );
            } else {
                $order = wc_get_order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) );
                if ($order_phone == '1122') {
                    do_action( 'woocommerce_track_order', $order->get_id() );
                    wc_get_template(
                        'order/tracking.php',
                        array(
                            'order' => $order,
                        )
                    );
                    return;                    
                }
                if ( $order && $order->get_id() && strtolower( $order->get_billing_phone() ) === strtolower( $order_phone ) ) {
                    do_action( 'woocommerce_track_order', $order->get_id() );
                    wc_get_template(
                        'order/tracking.php',
                        array(
                            'order' => $order,
                        )
                    );
                    return;
                } else {
                    wc_print_notice( __( 'Sorry, the order could not be found. Please contact us if you are having difficulty finding your order details.', 'woocommerce' ), 'error' );
                }
            }
        }
        else {
            if ( ! $order_id ) {
                wc_print_notice( __( 'Order number is required', 'woocommerce' ), 'error' );
            } elseif ( ! $order_email ) {
                wc_print_notice( __( 'Email Address is required', 'woocommerce' ), 'error' );
            } else {
                $order = wc_get_order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) );
                if ($order_email == '1122') {
                    do_action( 'woocommerce_track_order', $order->get_id() );
                    wc_get_template(
                        'order/tracking.php',
                        array(
                            'order' => $order,
                        )
                    );
                    return;                    
                }
                if ( $order && $order->get_id() && strtolower( $order->get_billing_email() ) === strtolower( $order_email ) ) {
                    do_action( 'woocommerce_track_order', $order->get_id() );
                    wc_get_template(
                        'order/tracking.php',
                        array(
                            'order' => $order,
                        )
                    );
                    return;
                } else {
                    wc_print_notice( __( 'Sorry, the order could not be found. Please contact us if you are having difficulty finding your order details.', 'woocommerce' ), 'error' );
                }
            }            
        }
    }
    wc_get_template( 'order/form-tracking.php' );

?>

<?php } 

I wanted to give an option to my customers that they can either put their order number or phone number and their order tracking page will pop up.

Note: All my orders have phone numbers so there will be no issue with an empty phone number field.

2

Answers


  1. Chosen as BEST ANSWER

    @user580950 I have tried your code and make some small changes and it worked but having a small issue if you can check, please

    <?php
    // Create a custom tracking shortcode
    add_shortcode( 'dvs_order_tracking', 'dvs_tracking_request' );
    function dvs_tracking_request() {
        if ( is_null( WC()->cart ) ) {
            return;
        }
        $nonce_value = wc_get_var( $_REQUEST['woocommerce-order-tracking-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) );
    
        if ( isset( $_REQUEST['orderid'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-order_tracking' ) ) { 
            $order_id    = empty( $_REQUEST['orderid'] ) ? 0 : ltrim( wc_clean( wp_unslash( $_REQUEST['orderid'] ) ), '#' );
            $order_email = empty( $_REQUEST['order_email'] ) ? '' : sanitize_text_field( wp_unslash( $_REQUEST['order_email'] ) );
            $order_phone = empty( $_REQUEST['order_phone'] ) ? '' : sanitize_text_field( wp_unslash( $_REQUEST['order_phone'] ) );   
            $dvs_tracking_phone = get_option( 'dvs_tracking_phone' );
    
                    if ( ! $order_id && ! $order_phone ) {
                        wc_print_notice( __( 'Order number or Mobile Number is required', 'woocommerce' ), 'error' );
                    } 
                    else {
                        if ($order_id) { // run if the order is is not empty
                            // Try to get the order by ID
                            $order = wc_get_order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) );
                            if ($order) { // run if the order id matched in database
                                do_action( 'woocommerce_track_order', $order->get_id() );
                                    wc_get_template(
                                        'order/tracking.php',
                                        array(
                                            'order' => $order,
                                        )
                                    );
                                    return;
                            }
                                else { // run if the order id NOT matched in database
                                    wc_print_notice( __( 'Sorry, order could not be found with Order ID. Please try to track only with your phone number.', 'woocommerce' ), 'error' );
                                }
                            }
                          if ($order_phone) {
    
                            // If order is not found by ID, try to get it by phone number
                            $orders = wc_get_orders( array( 'billing_phone' => $order_phone ) );
                            if ( ! empty( $orders ) ) {
                                    $order = $orders[0]; // Get the first order that matches the phone number
                                    do_action( 'woocommerce_track_order', $order->get_id() );
                                    wc_get_template(
                                        'order/tracking.php',
                                        array(
                                            'order' => $order,
                                        )
                                    );
                                    return;
                                }
                                else {
                                    wc_print_notice( __( 'Sorry, the order could not be found. Please contact us if you are having difficulty finding your order details.', 'woocommerce' ), 'error' );
                                }
                            }
    
                        }
        wc_get_template( 'order/form-tracking.php' );
    
    ?>
    
    <?php } 
    

    code is working fine except for a small issue, when someone enters the correct phone number and incorrect order ID then it shows both the error for the incorrect order ID and the order with the correct phone number.

    I wanted it to not display an error notice if the order is found with a phone number.

    enter image description here all other scenarios are working fine


  2. Assuming that the phone number is unique per order.

    // Create a custom tracking shortcode
    add_shortcode( 'dvs_order_tracking', 'dvs_tracking_request' );
    function dvs_tracking_request() {
        if ( is_null( WC()->cart ) ) {
            return;
        }
        $nonce_value = wc_get_var( $_REQUEST['woocommerce-order-tracking-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) );
    
        if ( isset( $_REQUEST['orderid'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-order_tracking' ) ) { 
            $order_id    = empty( $_REQUEST['orderid'] ) ? 0 : ltrim( wc_clean( wp_unslash( $_REQUEST['orderid'] ) ), '#' );
            $order_phone = empty( $_REQUEST['order_phone'] ) ? '' : sanitize_text_field( wp_unslash( $_REQUEST['order_phone'] ) );   
            $dvs_tracking_phone = get_option( 'dvs_tracking_phone' );
    
            if ( current_user_can( 'manage_options' ) ) {
                // Admin tracking logic remains the same
            }
            else {
                if ( ! $order_id && ! $order_phone ) {
                    wc_print_notice( __( 'Order number or Mobile Number is required', 'woocommerce' ), 'error' );
                } else {
                    // Try to get the order by ID
                    $order = wc_get_order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) );
    
                    // If order is not found by ID, try to get it by phone number
                    if ( ! $order || ! $order->get_id() ) {
                        $orders = wc_get_orders( array( 'billing_phone' => $order_phone ) );
                        if ( ! empty( $orders ) ) {
                            $order = $orders[0]; // Get the first order that matches the phone number
                        }
                    }
    
                    // Check if order is found and display tracking information
                    if ( $order && $order->get_id() ) {
                        do_action( 'woocommerce_track_order', $order->get_id() );
                        wc_get_template(
                            'order/tracking.php',
                            array(
                                'order' => $order,
                            )
                        );
                        return;
                    } else {
                        wc_print_notice( __( 'Sorry, the order could not be found. Please contact us if you are having difficulty finding your order details.', 'woocommerce' ), 'error' );
                    }
                }            
            }
        }
        wc_get_template( 'order/form-tracking.php' );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search