Настроить страницу получения заказа в зависимости от способа доставки в WooCommerce

Как я могу настроить страницу благодарности за заказ в зависимости от способа доставки заказа? Так, например, если клиент использовал опцию «Доставка по запросу», на странице благодарности будет отображаться другой заголовок.

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

person Shaun    schedule 16.02.2018    source источник


Ответы (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 вашей активной дочерней темы (или активной темы). Проверено и работает.

Аналогично: Добавление пользовательского сообщение на странице «Спасибо» по способу доставки в Woocommerce

person LoicTheAztec    schedule 17.02.2018