Собираем различные полезные и интересные решения связанные с личным кабинетом Woocommerce, а также формой входа в ЛК.
Логин WC в шапке
Создаем функцию ссылки в личный кабинет WC
function my_account_loginout_link() { if (is_user_logged_in() ) { global $wp; $current_user = get_user_by( 'id', get_current_user_id() ); echo '<a class="nav-link" href="'. wp_logout_url( get_permalink( wc_get_page_id( 'shop' ) ) ) .'">выйти</a>'; echo '<strong><a class="nav-link" href="'. get_permalink( wc_get_page_id( 'myaccount' ) ) .'">'.$current_user->display_name.'</a></strong>'; } elseif (!is_user_logged_in() ) { echo '<a class="nav-link" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Авторизация/Регистрация</a>'; } }
Выводим в любом месте сайта:
<?php my_account_loginout_link(); ?>
Пункты меню в личном кабинете
Удаляем любые из пунктов меню ЛК:
add_filter ( 'woocommerce_account_menu_items', 'misha_remove_my_account_links' ); function misha_remove_my_account_links( $menu_links ){ //unset( $menu_links['edit-address'] ); // Addresses //unset( $menu_links['dashboard'] ); // Remove Dashboard //unset( $menu_links['payment-methods'] ); // Remove Payment Methods //unset( $menu_links['orders'] ); // Remove Orders //unset( $menu_links['downloads'] ); // Disable Downloads //unset( $menu_links['edit-account'] ); // Remove Account details tab //unset( $menu_links['customer-logout'] ); // Remove Logout link return $menu_links; }
Переименование вкладок:
add_filter ( 'woocommerce_account_menu_items', 'misha_rename_downloads' ); function misha_rename_downloads( $menu_links ){ $menu_links['downloads'] = 'My Files'; return $menu_links; }
Добавление произвольной вкладки с ссылкой:
add_filter ( 'woocommerce_account_menu_items', 'misha_one_more_link' ); function misha_one_more_link( $menu_links ){ // we will hook "anyuniquetext123" later $new = array( 'anyuniquetext123' => 'Gift for you' ); // or in case you need 2 links // $new = array( 'link1' => 'Link 1', 'link2' => 'Link 2' ); // array_slice() is good when you want to add an element between the other ones $menu_links = array_slice( $menu_links, 0, 1, true ) + $new + array_slice( $menu_links, 1, NULL, true ); return $menu_links; } add_filter( 'woocommerce_get_endpoint_url', 'misha_hook_endpoint', 10, 4 ); function misha_hook_endpoint( $url, $endpoint, $value, $permalink ){ if( $endpoint === 'anyuniquetext123' ) { // ok, here is the place for your custom URL, it could be external $url = '/favorite/'; } return $url; }
Добавление произвольной вкладки с содержимым:
// Add Link (Tab) to My Account menu add_filter ( 'woocommerce_account_menu_items', 'misha_log_history_link', 40 ); function misha_log_history_link( $menu_links ){ $menu_links = array_slice( $menu_links, 0, 5, true ) + array( 'log-history' => 'Log history' ) + array_slice( $menu_links, 5, NULL, true ); return $menu_links; } // Register Permalink Endpoint add_action( 'init', 'misha_add_endpoint' ); function misha_add_endpoint() { // WP_Rewrite is my Achilles' heel, so please do not ask me for detailed explanation add_rewrite_endpoint( 'log-history', EP_PAGES ); } // woocommerce_account_{ENDPOINT NAME}_endpoint add_action( 'woocommerce_account_log-history_endpoint', 'misha_my_account_endpoint_content' ); function misha_my_account_endpoint_content() { // of course you can print dynamic content here, one of the most useful functions here is get_current_user_id() echo 'Last time you logged in: yesterday from Safari.'; }
Чтобы вкладка заработала необходимо пересохранить Постоянные ссылки.
Чтобы изменить заголовок страницы личного кабинета используем функцию:
// Изменить заголовки страниц-вкладок в личном кабинете function wpb_woo_endpoint_title( $title, $id ) { if ( is_wc_endpoint_url( 'favorites' ) && in_the_loop() ) { $title = "Избранное"; } elseif ( is_wc_endpoint_url( 'orders' ) && in_the_loop() ) { $title = "My Orders"; } elseif ( is_wc_endpoint_url( 'edit-account' ) && in_the_loop() ) { $title = "Change My Details2"; } return $title; } add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );
А чтобы фильтр is_wc_endpoint_url действовал на произвольные эндпоинты, то их нужно добавить к этому фильтру:
add_filter("woocommerce_get_query_vars", function ($vars) { foreach (["properties", "favorites", "help"] as $e) { $vars[$e] = $e; } return $vars; });
Хуки до и после блока с вкладками
<? php add_action ( 'woocommerce_before_account_navigation' , 'misha_some_content_before' ) ; function misha_some_content_before ( ) { echo 'выводим что-либо до блока с вкладками' ; } add_action ( 'woocommerce_after_account_navigation' , 'misha_some_content_after' ) ; function misha_some_content_after ( ) { ?> <p>выводим что-либо после блока с вкладками</ p> <? php }