根据订单状态更改 WooCommerce 感谢页面标题

时间:2021-03-08 15:39:35

标签: php wordpress woocommerce endpoint orders

尝试根据特定订单状态更改感谢页面的标题,方法如下:

add_action( 'woocommerce_thankyou', 'order_thank_you_status' );

function order_thank_you_status( $order_id ){

    // Get an instance of the `WC_Order` Object
    $order = wc_get_order( $order_id );
    // Get the order number
    $order_number  = $order->get_order_number();
    // Get the order status name
    $status_name  = wc_get_order_status_name( $order->get_status() );
    // Get the order key
    $test_order_key = $order->get_order_key();

    if ( $order->has_status('on-hold') || $order->has_status('mockup-requested') || $order->has_status('mockup-sent')|| $order->has_status('mockup-approved')) {
        echo 'helllo world';

        add_filter( 'the_title', 'woo_title_order_received', 10, 2 );
        function woo_title_order_received( $title, $id ) {
            if ( function_exists( 'is_order_received_page' ) &&
                is_order_received_page() && get_the_ID() === $id ) {
                $title = "Mockup request received";
            }
            return $title;
        }
    }
}

但我尝试了多种方法将两者结合起来,但都没有成功。

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容根据订单状态更改“已收到订单”页面标题:

add_action( 'the_title', 'change_order_received_page_title_based_on_order_status' );
function change_order_received_page_title_based_on_order_status( $title ){
    if ( is_wc_endpoint_url('order-received') && $title === 'Order received' ) {
        global $wp;

        $targeted_statuses = array('on-hold', 'mockup-requested', 'mockup-sent', 'mockup-approved' );

        // Get an instance of the `WC_Order` Object
        $order = wc_get_order( absint($wp->query_vars['order-received']) );

        if ( is_a( $order, 'WC_Order' ) && in_array( $order->get_status(), $targeted_statuses ) ) {
            return __("Mockup request received", "woocommerce");
        }
    }
    return $title;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。

相关问题