更改订单在 WooCommerce 中收到标题问题

Change Order received title issue in WooCommerce

我有以下代码可以工作并更改 'order received' 页面的标题:

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 = "Thank you, good luck!";
    }
    return $title;
}

然而,由于 Too few arguments to function woo_title_order_received(),它会导致商店页面出现致命错误。上网一查发现the_title不对,应该是get_the_title。如果我将其更改为致命错误消失,但它不再更改订单接收页面上的标题。

None 我在网上找到的其他片段都有效,但我不明白为什么上面的内容会阻止商店页面工作。有什么想法吗?

不知道为什么要使用 the_title WordPress 钩子和多个 if 条件,而 WooCommerce 有特定的钩子。

'woocommerce_endpoint_' . $endpoint . '_title' 过滤器挂钩允许更改 Order received 标题。

所以你得到:

/**
 * @param string $title Default title.
 * @param string $endpoint Endpoint key.
 * @param string $action Optional action or variation within the endpoint.
 */
function filter_woocommerce_endpoint_order_received_title( $title, $endpoint, $action ) {   
    $title = __( 'Thank you, good luck!', 'woocommerce' );
 
    return $title;
}
add_filter( 'woocommerce_endpoint_order-received_title', 'filter_woocommerce_endpoint_order_received_title', 10, 3 );

尝试将 $id 参数设置为 null (未定义时有用):

add_filter( 'the_title', 'woo_title_order_received', 10, 2 );
function woo_title_order_received( $title, $id = null ) {
    if ( function_exists( 'is_order_received_page' ) &&
         is_order_received_page() && get_the_ID() === $id ) {
        $title = "Thank you, good luck!";
    }
    return $title;
}

它可以工作…