Créer des points de terminaisons supplémentaires dans l’espace Mon Compte de WooCommerce

<?php
/**
 *  WooCommerce - Account - Navigation
 */
add_filter( 'woocommerce_account_menu_items', '_pit_woo_account_nav_links', 0, 1 );
function _pit_woo_account_nav_links( $menu_links ) {

    $menu_links = array();

    $menu_links['orders']              = __( 'Mes commandes', 'pilot-in' );
    $menu_links['bons-de-livraison']   = __( 'Mes bons de livraison', 'pilot-in' ); // custom
    $menu_links['factures']            = __( 'Mes factures', 'pilot-in' ); // custom
    $menu_links['devis']               = __( 'Mes devis', 'pilot-in' ); // custom
    $menu_links['historique-produits'] = __( 'Mon historique', 'pilot-in' ); // custom
    $menu_links['edit-account']        = __( 'Mon profil', 'pilot-in' );
    $menu_links['customer-logout']     = __( 'Se déconnecter', 'pilot-in' );

    unset( $menu_links['edit-address'] );
    unset( $menu_links['dashboard'] );
    unset( $menu_links['downloads'] );

    return $menu_links;
}

/**
 *  WooCommerce - Endpoints
 *  - List of custom endpoints we add
 */
$woocommerce_endpoints_custom = array(
    'bons-de-livraison',    // my-account/bons-de-livraison
    'factures',             // my-account/factures
    'devis',                // my-account/devis
    'historique-produits',  // my-account/historique-produits
);

/**
 *  WooCommerce - Account - Query Vars
 *  - Add query vars to WooCommerce
 */
add_filter( 'woocommerce_get_query_vars', '_pit_woo_account_query_vars' );
function _pit_woo_account_query_vars( $query_vars ) {

    global $woocommerce_endpoints_custom;
    if ( !$woocommerce_endpoints_custom || empty( $woocommerce_endpoints_custom ) ) {
        return $query_vars;
    }

    foreach ( $woocommerce_endpoints_custom as $endpoint ) {
        $query_vars[ $endpoint ] = $endpoint;
    }

    return $query_vars;
}

/**
 *  WooCommerce - Account - Endpoints
 *  - Create custom endpoints (ex: mon-compte/livraisons)
 */
add_action( 'init', '_pit_woo_account_custom_endpoints' );
function _pit_woo_account_custom_endpoints() {

    global $woocommerce_endpoints_custom;
    if ( !$woocommerce_endpoints_custom || empty( $woocommerce_endpoints_custom ) ) {
        return;
    }

    foreach ( $woocommerce_endpoints_custom as $endpoint ) {
        add_rewrite_endpoint( $endpoint, EP_PAGES );
    }
}

/**
 *  WooCommerce - Account - Title
 *  - Display WooCommerce Account title based on wc endpoint url
 */
add_action( 'woocommerce_account_content', '_pit_woo_account_title', 0 );
function _pit_woo_account_title() {

    if ( !is_wc_endpoint_url() ) {
        return;
    }

    $title = get_the_title();

    // Commandes
    if ( is_wc_endpoint_url( 'orders' ) ) {
        $title = __( 'Mes commandes', 'pilot-in' );
    }

    // Factures
    elseif ( is_wc_endpoint_url( 'factures' ) ) {
        $title = __( 'Mes factures', 'pilot-in' );
    }

    // Devis
    elseif ( is_wc_endpoint_url( 'devis' ) ) {
        $title = __( 'Mes devis', 'pilot-in' );
    }

    // Bons de livraison
    elseif ( is_wc_endpoint_url( 'bons-de-livraison' ) ) {
        $title = __( 'Mes bons de livraison', 'pilot-in' );
    }

    // Historique produits
    elseif ( is_wc_endpoint_url( 'historique-produits' ) ) {
        $title = __( 'Mon historique', 'pilot-in' );
    }

    ?>
<h1><?php echo $title; ?></h1>
    <?php
}


/**
 *  WooCommerce - Account - Endpoint
 *  - Get dynamic template based on endpoint if no custom endpoint action is set.
 */
foreach ( $woocommerce_endpoints_custom as $endpoint ) {

    if (has_action('woocommerce_account_' . $endpoint . '_endpoint')) {
        continue;
    }

    add_action(
        'woocommerce_account_' . $endpoint . '_endpoint',
        function() use ( $endpoint ) {

            $query_var_data = array();  // Replace with function to return query_var_data for current logged in user

            wc_get_template(
                "myaccount/$endpoint.php",
                array(
                    $endpoint => $query_var_data,
                )
            );

        }
    );
}

 

Déclinaison de logo : son importance dans la charte graphique

Site de police d’écriture : notre sélection pour le digital !

WordPress vs Symfony : lequel choisir pour mon projet ?