How to add a new endpoint in woocommerce

Im using woocommerce for a website and where the client sells software. One of the options I have to add is request a license button on my account page.

I already have the function that does this in a file request-license.php in the woocommerce folder in my theme but I’m having problems to add a new point.

enter image description here

when you click view the endpoint calls view-order.php file so I want to call request-license when the request license button is clicked.

here is how is called

 <?php
    $actions = array();

    if ( in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order ) ) ) {
            $actions['pay'] = array(
            'url'  => $order->get_checkout_payment_url(),
            'name' => __( 'Pay', 'woocommerce' )
        );
    }

    if ( in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) {
        $actions['cancel'] = array(
            'url'  => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
            'name' => __( 'Cancel', 'woocommerce' )
        );
    }

    $actions['license'] = array(
        'url'  => $order->get_request_license_url(),
        'name' => __( 'Request License', 'woocommerce' )
    );

    $actions['view'] = array(
        'url'  => $order->get_view_order_url(),
        'name' => __( 'View', 'woocommerce' )
    );

    $actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order );

    if ( $actions ) {
        foreach ( $actions as $key => $action ) {
            echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>';
        }
    }
?>

I know I have to create the get_request_license_url() function but not sure how to implement it. Hope I can have some help here

1
1

seems woocommerce doesn’t have any filters when registering their endpoints,
https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-query.php#L84

so you need to add your new endpoint on init hooks, just like this

add_action( 'init', 'add_endpoint' );
function add_endpoint(){
    add_rewrite_endpoint( 'license', EP_ROOT | EP_PAGES );
}

then you have to do some filtering on wc_get_template to call your files when the request match your endpoint

add_filter( 'wc_get_template', 'custom_endpoint', 10, 5 );
function custom_endpoint($located, $template_name, $args, $template_path, $default_path){

    if( $template_name == 'myaccount/my-account.php' ){
        global $wp_query;
        if(isset($wp_query->query['license'])){
            $located = get_template_directory() . '/your-path-to-file.php';
        }
    }

    return $located;
}

so when you visit my account page with endpoint license,
let say http://yourdomain.com/my-account/license/, that will display your custom code

Leave a Comment