Customize Order received page based on shipping me

2019-06-01 06:24发布

How could I customise the order thank you page based on the order's shipping method? So for example if a customer used 'Delivery on request' option, the thank you page would display a different title.

add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
    if ( is_order_received_page() && get_the_ID() === $id ) {
        global $wp;
        // Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
        $order_id  = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
        $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
        if ( $order_id > 0 ) {
            $order = wc_get_order( $order_id );
            if ( $order->get_order_key() != $order_key ) {
                $order = false;
            }
        }
        if ( isset ( $order ) ) {
    $chosen_titles = array();
    $available_methods = $wp->shipping->get_packages();
    $chosen_rates = ( isset( $wp->session ) ) ? $wp->session->get( 'chosen_shipping_methods' ) : array();

    foreach ($available_methods as $method)
           foreach ($chosen_rates as $chosen) {
                if( isset( $method['rates'][$chosen] ) ) $chosen_titles[] = $method['rates'][ $chosen ]->label;
            }
    if( in_array( 'Delivery price on request', $chosen_titles ) ) {
     //$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
        $title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
    }

        }
    }
    return $title;
}

1条回答
放我归山
2楼-- · 2019-06-01 06:41

is_order_received_page() doesn't exist. Instead use is_wc_endpoint_url( 'order-received' )… Also $wp->session or $wp->shipping will not work. Instead you can find the chosen shipping method data in the order item "shipping".

Try this instead:

add_filter( 'the_title', 'customizing_order_received_title', 10, 2 );
function customizing_order_received_title( $title, $post_id ) {
    if ( is_wc_endpoint_url( 'order-received' ) && get_the_ID() === $post_id ) {
        global $wp;

        $order_id  = absint( $wp->query_vars['order-received'] );
        $order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';

        if ( empty($order_id) || $order_id == 0 )
            return $title; // Exit

        $order = wc_get_order( $order_id );

        if ( $order->get_order_key() != $order_key )
            return $title; // Exit

        $method_title_names = array();

        // Loop through Order shipping items data and get the method title
        foreach ($order->get_items('shipping') as $shipping_method )
            $method_title_names[] = $shipping_method->get_name();

        if( in_array( 'Delivery price on request', $method_title_names ) ) {
            $title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
        }
    }
    return $title;
}

Code goes on function.php file of your active child theme (or active theme). Tested and works.

Similar: Adding custom message on Thank You page by shipping method in Woocommerce

查看更多
登录 后发表回答