目前,當您詳細查看訂單時,WooCommerce“我的訂單”選項卡下的 URL 等于/my-account/view-order/ORDER-ID/
我想將此網址更改為/my-account/orders/ORDER-ID/ - 所以:
當前網址:/my-account/view-order/ORDER-ID/
新網址:/my-account/orders/ORDER-ID/
因此,通過 WooCommerce 設定,我將查看訂單端點更改為訂單
這行得通,詳細查看訂單的按鈕的 url 已有效更改,只有我在訂單詳細資訊頁面上收到以下訊息
'還沒有下訂單'。
我相信通過以下代碼,可以在/plugins/woocommerce/includes/class-wc-order.php | 第 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 );
}
我可以實作我的目標,但我可以使用一些建議。誰能指導我?
uj5u.com熱心網友回復:
將/my-account/view-order/ORDER-ID/ url 更改為/my-account/orders/ORDER-ID/可以通過幾個步驟來完成。
步驟 1)更改查看訂單端點
可以在頁面設定部分的 WooCommerce > 設定 > 高級中自定義每個端點的 URL

或者您使用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 );
雖然端點現在已更改,但您在查看新 url 時將看到以下訊息:
'還沒有下訂單'。
這是因為加載了/myaccount/orders.php模板檔案,而這應該是/myaccount/view-order.php模板
步驟 2)提供正確的模板檔案已加載,這適用于/my-account/orders/和/my-account/orders/ORDER-ID/ url。
因此我們需要覆寫現有的woocommerce_account_orders()函式,該函式可以在/includes/wc-template-functions.php檔案中找到。這樣我們的自定義函式就被執行了
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 >' . esc_html__( 'Invalid order.', 'woocommerce' ) . ' <a href="' . esc_url( wc_get_page_permalink( 'myaccount' ) ) . '" >' . 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,
)
);
}
}
重要提示:我們無意通過更改核心檔案來更改此功能!!您可以簡單地將其添加到functions.php,這是因為此函式是由if ( ! function_exists( 'woocommerce_account_orders' ) ) {
步驟 3)此步驟是可選的。這可確保/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 );
步驟 1、2 和 3中的代碼位于活動子主題(或活動主題)的functions.php檔案中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/476057.html
標籤:php WordPress woocommerce 端点 订单
