根据WooCommerce中的送货方式自定义订单接收页面

时间:2018-02-16 14:15:33

标签: php wordpress woocommerce checkout orders

如何根据订单的送货方式自定义订单谢谢页面?因此,例如,如果客户使用“按要求交付”'选项,谢谢页面将显示不同的标题。

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 个答案:

答案 0 :(得分:1)

is_order_received_page()不存在。而是使用is_wc_endpoint_url( 'order-received' ) ... $wp->session$wp->shipping也不起作用。您可以在订单商品“送货”中找到所选的送货方式数据。

请改为尝试:

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;
}

代码在您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

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