【问题标题】:Customize Order received page based on shipping method in WooCommerce根据 WooCommerce 中的运输方式自定义订单接收页面
【发布时间】:2018-07-27 11:23:50
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress woocommerce checkout orders


    【解决方案1】:

    is_order_received_page() 不存在。而是使用is_wc_endpoint_url( 'order-received' )... $wp->session$wp->shipping 也不起作用。相反,您可以在订单项“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

    【讨论】:

      猜你喜欢
      • 2018-09-14
      • 2019-02-09
      • 2018-02-15
      • 2018-06-26
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 2022-11-02
      相关资源
      最近更新 更多