简体   繁体   中英

Custom Woocommerce Tracking Form

Woocommerce has a default tracking form that lets customers see their order status.

But I'm looking to replace the Email request with Billing Phone Number (which value should be billing_phone )

Any idea how to change? It seems it's not that easy to change order_email with billing_phone .

Here is the part in woocommerce/order/tracking-form.php

<p class="form-row form-row-first">
    <label for="orderid">
        <?php esc_html_e( 'Order ID', 'woocommerce' ); ?>
    </label>
    <input class="input-text" type="text" name="orderid" id="orderid" value="<?php echo isset( $_REQUEST['orderid'] ) ? esc_attr( wp_unslash( $_REQUEST['orderid'] ) ) : ''; ?>" placeholder="<?php esc_attr_e( 'Found in your order confirmation email.', 'woocommerce' ); ?>" />
</p>
<?php // @codingStandardsIgnoreLine ?>
<p class="form-row form-row-last">
    <label for="order_email">
        <?php esc_html_e( 'Billing email', 'woocommerce' ); ?>
    </label>
    <input class="input-text" type="text" name="order_email" id="order_email" value="<?php echo isset( $_REQUEST['order_email'] ) ? esc_attr( wp_unslash( $_REQUEST['order_email'] ) ) : ''; ?>" placeholder="<?php esc_attr_e( 'Email you used during checkout.', 'woocommerce' ); ?>" />
</p>
<?php // @codingStandardsIgnoreLine ?>
<div class="clear"></div>

Thank you all.

There is no simple solution for this. Since the core WC process is to get the email input and to compare it against the order billing email. Unfortunately, I could not find an easy way to override it. But you can consider adjusting the copy of the template file in your theme folder to get a response from your code rather than WC.

to do this, you will need:

  1. Change the nonce field in the form, so the action will be customized (then you will be able to "catch" and handle it from the server-side:

wp_nonce_field( 'woocommerce-order_tracking_custom', 'woocommerce-order-tracking-custom-nonce' );

  1. Copy (to your functions.php) and adjust the method output from class-wc-shortcode-order-tracking.php

public static function output( $atts ) {
            // Check cart class is loaded or abort.
            if ( is_null( WC()->cart ) ) {
                return;
            }
...
...
        

wc_get_template( 'order/form-tracking.php' );

}

To something like this:

add_action('init', 'handle_tracking_request');
function handle_tracking_request(  ) {
        // Check cart class is loaded or abort.
        if ( is_null( WC()->cart ) ) {
            return;
        }

        $nonce_value = wc_get_var( $_REQUEST['woocommerce-order-tracking-custom-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) );

        if ( isset( $_REQUEST['orderid'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-order_tracking_custom' ) ) { 

            $order_id    = empty( $_REQUEST['orderid'] ) ? 0 : ltrim( wc_clean( wp_unslash( $_REQUEST['orderid'] ) ), '#' ); // WPCS: input var ok.
            $order_phone = empty( $_REQUEST['order_phone'] ) ? '' : sanitize_email( wp_unslash( $_REQUEST['order_phone'] ) ); 

            if ( ! $order_id ) {
                wc_print_notice( __( 'Please enter a valid order ID', 'woocommerce' ), 'error' );
            } elseif ( ! $order_phone ) {
                wc_print_notice( __( 'Please enter a valid phone', 'woocommerce' ), 'error' );
            } else {
                $order = wc_get_order( apply_filters( 'woocommerce_shortcode_order_tracking_order_id', $order_id ) );

                if ( $order && $order->get_id() && ( $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' );
                }
            }
        }

        wc_get_template( 'order/form-tracking.php' );
    }

I didn't tested it, and hope it will work. You may need to adjust it further. let me know if it helped you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM