I am using this woocommerce default code for tracking where customer have to enter both order id and phone number to track the order.
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
@user580950 I have tried your code and make some small changes and it worked but having a small issue if you can check, please
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.
all other scenarios are working fine
Assuming that the phone number is unique per order.