最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Woocommerce My Account Endpoint - how to get ID parameter from URL?

programmeradmin0浏览0评论

I'm using WooCommerce v3.2.6 and WordPress 4.9.1. I've added an endpoint to the WooCommerce myaccount area (view-subscription):

function my_custom_endpoints() {
    add_rewrite_endpoint( 'view-subscription', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'my_custom_endpoints' );

function my_custom_query_vars( $vars ) {
    $vars[] = 'view-subscription';
    return $vars;
}

add_filter( 'query_vars', 'my_custom_query_vars', 0 );

function view_subscription_endpoint_content() {
    include get_template_directory().'/woocommerce/myaccount/view-subscription.php'; 
}

add_action( 'woocommerce_account_view-subscription_endpoint', 'view_subscription_endpoint_content' );

The endpoint is working but I want to be able to pass the ID of a subscription (a post type) to the endpoint (similar to how view-order works). How can I do this?

eg.

myaccount/view-order/21313 - Displays details of order #21313
myaccount/view-subscription/35464 - I want this to display the details of the subscription post #35464.

If I go to the above URL myaccount/view-subscription/35464 , the view-subscription.php template is still loading, but what is the best way to access the ID, 35464, from the URL?

I'm using WooCommerce v3.2.6 and WordPress 4.9.1. I've added an endpoint to the WooCommerce myaccount area (view-subscription):

function my_custom_endpoints() {
    add_rewrite_endpoint( 'view-subscription', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'my_custom_endpoints' );

function my_custom_query_vars( $vars ) {
    $vars[] = 'view-subscription';
    return $vars;
}

add_filter( 'query_vars', 'my_custom_query_vars', 0 );

function view_subscription_endpoint_content() {
    include get_template_directory().'/woocommerce/myaccount/view-subscription.php'; 
}

add_action( 'woocommerce_account_view-subscription_endpoint', 'view_subscription_endpoint_content' );

The endpoint is working but I want to be able to pass the ID of a subscription (a post type) to the endpoint (similar to how view-order works). How can I do this?

eg.

myaccount/view-order/21313 - Displays details of order #21313
myaccount/view-subscription/35464 - I want this to display the details of the subscription post #35464.

If I go to the above URL myaccount/view-subscription/35464 , the view-subscription.php template is still loading, but what is the best way to access the ID, 35464, from the URL?

Share Improve this question edited Jan 24, 2018 at 13:49 Sarah asked Jan 24, 2018 at 13:12 SarahSarah 4451 gold badge6 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

I hope it's not too late, but anyway, I know it will help someone else.

echo get_query_var('your-endpoint');

So, for your code, it will be:

echo get_query_var('view-subscription');

Please use this, it will help me according same as view-order link.

wc_get_endpoint_url()

eg.

myaccount/view-order/21313 - Displays details of order #21313
myaccount/view-coupon/123 - Display details of coupon #123

发布评论

评论列表(0)

  1. 暂无评论