当前,当您详细查看订单时,在/my-account/view-order/ORDER-ID/ 'My‘选项卡下的WooCommerce’My orders‘选项卡下的URL等于WooCommerce。
我想将这个url更改为/my-account/orders/ORDER-ID/ -所以:
/my-account/view-order/ORDER-ID/:
/my-account/orders/ORDER-ID/:
因此,通过WooCommerce设置,我将视图顺序端点更改为orders。
这样做,查看详细订单的按钮的url已经有效地更改了,但我在order页面上得到了以下消息
‘还没有下订单’。
我相信下面的代码,可以在/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 );
}
我可以实现我的目标,但我需要一些建议。有人能指引我吗?
发布于 2022-05-15 01:09:15
可以通过应用几个步骤将/my-account/view-order/ORDER-ID/ url更改为/my-account/orders/ORDER-ID/。
步骤1)更改视图顺序端点
每个端点的URL可以在页面设置部分的WooCommerce > Settings > Advanced中进行自定义
或--您使用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模板。
/my-account/orders/步骤2)提供正确的模板文件加载,这对于/my-account/orders/ORDER-ID/和 url都是如此。
因此,我们需要覆盖现有的woocommerce_account_orders()
函数,该函数可以在/include/wc-template-Funcs.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 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,
)
);
}
}
重要:它不是通过改变核心文件来改变这个功能的!!您可以简单地将它添加到functions.php中,这是因为这个函数是由if ( ! function_exists( 'woocommerce_account_orders' ) ) {
嵌入的。
步骤3)这个步骤是可选的。这将确保无法再访问/my-account/view-order/ORDER-ID/ url (现在是旧的url)。
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文件中。
相关:Change title custom "view-order" endpoint in WooCommerce My account
https://stackoverflow.com/questions/72239000
复制相似问题