[英]Change "view-order/order-id" url/endpoint in WooCommerce My account - orders to "orders/order-id"
Currently the URL, under the WooCommerce 'My orders' tab, for the button when you view an order in detail is equal to /my-account/view-order/ORDER-ID/目前,当您详细查看订单时,WooCommerce“我的订单”选项卡下的 URL 等于/my-account/view-order/ORDER-ID/
I would like to change this url to /my-account/orders/ORDER-ID/ - So:我想将此网址更改为/my-account/orders/ORDER-ID/ - 所以:
current url: /my-account/view-order/ORDER-ID/当前网址: /my-account/view-order/ORDER-ID/
new url: /my-account/orders/ORDER-ID/新网址: /my-account/orders/ORDER-ID/
Therefore, via the WooCommerce settings I changed the view-order endpoint to orders因此,通过 WooCommerce 设置,我将查看订单端点更改为订单
This works, the url of the button to view the order in detail has effectively been changed, only I get the following message on the order detail page这行得通,详细查看订单的按钮的 url 已有效更改,只有我在订单详细信息页面上收到以下消息
'No order has been made yet'.
'还没有下订单'。
I believe through the following code, which can be found in /plugins/woocommerce/includes/class-wc-order.php |我相信通过以下代码,可以在/plugins/woocommerce/includes/class-wc-order.php | line 1675
第 1675 行
/**
* Generates a URL to view an order from the my account page.
*
* @return string
*/
public function get_view_order_url() {
return apply_filters( 'woocommerce_get_view_order_url', wc_get_endpoint_url( 'view-order', $this->get_id(), wc_get_page_permalink( 'myaccount' ) ), $this );
}
I can achieve my goal, but I could use some advice.我可以实现我的目标,但我可以使用一些建议。 Anyone who can guide me?
谁能指导我?
Changing the /my-account/view-order/ORDER-ID/ url to /my-account/orders/ORDER-ID/ can be done by applying a few steps.将/my-account/view-order/ORDER-ID/ url 更改为/my-account/orders/ORDER-ID/可以通过几个步骤来完成。
Step 1) change the view order endpoint步骤 1)更改查看订单端点
The URL for each endpoint can be customized in WooCommerce > Settings > Advanced in the page setup section可以在页面设置部分的 WooCommerce > 设置 > 高级中自定义每个端点的 URL
OR you use the woocommerce_get_endpoint_url
filter hook instead或者您使用
woocommerce_get_endpoint_url
过滤器挂钩
function filter_woocommerce_get_endpoint_url( $url, $endpoint, $value, $permalink ) {
// Specific endpoint
if ( $endpoint === 'view-order' ) {
// New URL
$url = $permalink . 'orders/' . $value;
}
return $url;
}
add_filter( 'woocommerce_get_endpoint_url', 'filter_woocommerce_get_endpoint_url', 10, 4 );
While the endpoint is now changed, you will see the following message when viewing the new url:虽然端点现在已更改,但您在查看新 url 时将看到以下消息:
'No order has been made yet'.
'还没有下订单'。
This is because the /myaccount/orders.php template file is loaded, while this should be the /myaccount/view-order.php template这是因为加载了/myaccount/orders.php模板文件,而这应该是/myaccount/view-order.php模板
Step 2) provide that the correct template file is loaded, this for both the /my-account/orders/ and /my-account/orders/ORDER-ID/ url.步骤 2)提供正确的模板文件已加载,这适用于/my-account/orders/和/my-account/orders/ORDER-ID/ url。
Therefore we need to overwrite the existing woocommerce_account_orders()
function, which can be found in the /includes/wc-template-functions.php file.因此我们需要覆盖现有的
woocommerce_account_orders()
函数,该函数可以在/includes/wc-template-functions.php文件中找到。 So that our custom function is executed这样我们的自定义函数就被执行了
function woocommerce_account_orders( $current_page ) {
global $wp;
// For view-order template file
if ( isset( $wp->query_vars['orders'] ) && is_numeric( $wp->query_vars['orders'] ) ) {
$order_id = $wp->query_vars['orders'];
$order = wc_get_order( $order_id );
if ( ! $order || ! current_user_can( 'view_order', $order_id ) ) {
echo '<div class="woocommerce-error">' . esc_html__( 'Invalid order.', 'woocommerce' ) . ' <a href="' . esc_url( wc_get_page_permalink( 'myaccount' ) ) . '" class="wc-forward">' . esc_html__( 'My account', 'woocommerce' ) . '</a></div>';
return;
}
// Backwards compatibility.
$status = new stdClass();
$status->name = wc_get_order_status_name( $order->get_status() );
wc_get_template(
'myaccount/view-order.php',
array(
'status' => $status, // @deprecated 2.2.
'order' => $order,
'order_id' => $order->get_id(),
)
);
} else {
$current_page = empty( $current_page ) ? 1 : absint( $current_page );
$customer_orders = wc_get_orders(
apply_filters(
'woocommerce_my_account_my_orders_query',
array(
'customer' => get_current_user_id(),
'page' => $current_page,
'paginate' => true,
)
)
);
wc_get_template(
'myaccount/orders.php',
array(
'current_page' => absint( $current_page ),
'customer_orders' => $customer_orders,
'has_orders' => 0 < $customer_orders->total,
)
);
}
}
Important: it is not the intention to change this function by changing the core file!!重要提示:我们无意通过更改核心文件来更改此功能!! you can simply add this to functions.php , this is because this function is embedded by
if ( ! function_exists( 'woocommerce_account_orders' ) ) {
你可以简单地将它添加到functions.php ,这是因为这个函数是由
if ( ! function_exists( 'woocommerce_account_orders' ) ) {
Step 3) this step is optional.步骤 3)此步骤是可选的。 This ensures that the /my-account/view-order/ORDER-ID/ url (which is now the old url) is no longer reachable
这可确保/my-account/view-order/ORDER-ID/网址(现在是旧网址)不再可访问
function action_woocommerce_account_view_order_endpoint() {
remove_action( 'woocommerce_account_view-order_endpoint', 'woocommerce_account_view_order' );
}
add_action( 'woocommerce_account_view-order_endpoint', 'action_woocommerce_account_view_order_endpoint', 1 );
Code from step 1, 2 and 3 goes in functions.php file of the active child theme (or active theme).步骤 1、2 和 3中的代码位于活动子主题(或活动主题)的functions.php文件中。
Related: Change title custom "view-order" endpoint in WooCommerce My account相关:在 WooCommerce 我的帐户中更改标题自定义“查看订单”端点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.