Come aggiungere un nuovo endpoint in WooCommerce
Sto utilizzando WooCommerce per un sito web dove il cliente vende software. Una delle opzioni che devo aggiungere è un pulsante per richiedere una licenza nella pagina del mio account.
Ho già la funzione che gestisce questa operazione in un file request-license.php nella cartella woocommerce del mio tema, ma ho problemi ad aggiungere un nuovo endpoint.
Quando si clicca su Visualizza, l'endpoint chiama il file view-order.php, quindi vorrei che chiamasse request-license.php quando si clicca sul pulsante Richiedi Licenza.
Ecco come viene chiamato:
<?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' => __( 'Paga', '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' => __( 'Annulla', 'woocommerce' )
);
}
$actions['license'] = array(
'url' => $order->get_request_license_url(),
'name' => __( 'Richiedi Licenza', 'woocommerce' )
);
$actions['view'] = array(
'url' => $order->get_view_order_url(),
'name' => __( 'Visualizza', '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>';
}
}
?>
So che devo creare la funzione get_request_license_url() ma non sono sicuro di come implementarla. Spero di poter ricevere qualche aiuto qui.

sembra che WooCommerce non abbia alcun filtro quando registra i suoi endpoint, https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-query.php#L84
quindi devi aggiungere il tuo nuovo endpoint sugli hook init, proprio così
add_action( 'init', 'add_endpoint' );
function add_endpoint(){
add_rewrite_endpoint( 'license', EP_ROOT | EP_PAGES );
}
poi devi fare un po' di filtraggio su wc_get_template
per chiamare i tuoi file quando la richiesta corrisponde al tuo 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;
}
quindi quando visiti la pagina del mio account con l'endpoint license
,
ad esempio http://yourdomain.com/my-account/license/
, verrà visualizzato il tuo codice personalizzato

Non sono sicuro da quale versione, ma WooCommerce ha un filtro per modificare le variabili di query: https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-query.php#L195
