简体   繁体   中英

Change title custom "view-order" endpoint in WooCommerce My account

I have added the title to all the pages of the account using the following code:

add_filter( 'the_title', 'wc_page_endpoint_title' );
the_title( '<h2>', '</h2>' );

Path: plugins/woocommerce/templates/myaccount/my-account.php

<?php
/**
 * My Account page
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/my-account.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 3.5.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * My Account navigation.
 *
 * @since 2.6.0
 */
do_action( 'woocommerce_account_navigation' ); ?>

<div class="woocommerce-MyAccount-content">
    <?php
        add_filter( 'the_title', 'wc_page_endpoint_title' );
        the_title( '<h2>', '</h2>' );
    ?>
    <?php
        /**
         * My Account content.
         *
         * @since 2.6.0
         */
        do_action( 'woocommerce_account_content' );
    ?>
</div>

I also changed the url of the view-order page using the following url :

Change "view-order/order-id" url/endpoint in WooCommerce My account - orders to "orders/order-id"

  • Title Before the change url view-order page : order # (order-id)

  • Title after the change url view-order page : orders

I want it to be the same as before

The following code does not work either:

function filter_woocommerce_endpoint_view_order_title( $title, $endpoint, $action ) {
   $title = __( 'test', 'woocommerce' );
   return $title;
}
add_filter( 'woocommerce_endpoint_view-order_title', 'filter_woocommerce_endpoint_view_order_title', 10, 3 );

With the changes you made, you also changed the endpoint. To make this run smoothly for that specific change, you must:

Replace:

<?php
    add_filter( 'the_title', 'wc_page_endpoint_title' );
    the_title( '<h2>', '</h2>' );
?>

With:

<?php
    function filter_the_title( $title, $id ) {
        global $wp;

        if ( isset( $wp->query_vars['orders'] ) && is_numeric( $wp->query_vars['orders'] ) ) {
            $order = wc_get_order( $wp->query_vars['orders'] );
            /* translators: %s: order number */
            $title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : '';
        } else {
            $title = wc_page_endpoint_title( $title );
        }

        return $title;
    }
    add_filter( 'the_title', 'filter_the_title', 10, 2 );
    the_title( '<h2>', '</h2>' );
?>

Related: Change "view-order/order-id" url/endpoint in WooCommerce My account - orders to "orders/order-id"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM