Come aggiungere un nuovo endpoint in WooCommerce

1 set 2015, 20:02:10
Visualizzazioni: 19K
Voti: 7

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.

Pulsante Richiedi Licenza in WooCommerce

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.

1
Commenti

Puoi trovare la tua risposta in questo link. https://github.com/woocommerce/woocommerce/wiki/Customising-%22My-Account%22-tabs

vijay vijay
22 feb 2017 14:29:44
Tutte le risposte alla domanda 1
1

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

11 dic 2015 15:20:51
Commenti

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

Marian Marian
8 mag 2019 10:38:37