Change Woocommerce "My Account" page title for unlogged users - php

I work on small project, and want to make this scenario to work on. I want to make when user is logged in the title of My Account page to write "My Account", and when is logged out, the title of page to write "Login Register". To explain better with images:
For logged in users
For non-logged in users
I have this code apply, but it seems do not change something.
function dynamic_label_change( $items, $args ) {
if ( ! is_user_logged_in() ) {
$items = str_replace( "My Account", "Account Login / Register ", $items );
}
return $items;
}
or maybe
// When user is on my account page and not logged in
if (is_account_page() && !is_user_logged_in()) {
echo '<h1 class="entry-title">'.__("My custom title",
"the_theme_slug").'</h1>'; // My custom title
} else {
the_title( '<h1 class="entry-title">', '</h1>' ); // the normal template
title
}
Any help?

You can use the following:
add_filter( 'the_title', 'display_product_image_in_order_item' );
function display_product_image_in_order_item( $title ) {
if( is_account_page() && $title === __('My Account', 'woocommerce') && ! is_user_logged_in() ) {
$title = __( 'Account Login / Register', 'woocommerce' );
}
return $title;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related

WordPress - Redirect logged in users from login page to custom page

I have created a page template that will be used to show a custom wordpress login form.
<?php
/*
* Template Name: Login
*/
get_header();
if( !is_user_logged_in() ){
?>
html code
<?php
} else {
$user = wp_get_current_user();
if( current_user_can('edit_users') && in_array('customrole', $user->roles) ){
wp_redirect( site_url('/role-page') );
exit;
}
if( current_user_can('edit_users') && in_array('customrole1', $user->roles) ){
wp_redirect( site_url('/role-page1') );
exit;
}
}
get_footer();
?>
I've noticed that if an user is logged in, the page will be loaded and is blank, the users will be not redirected to the desired location. How I can fix this problem and let the logged users be redirected based on their role?
Use template_redirect
add_action( 'template_redirect', function() {
if ( is_user_logged_in()){
$restricted = array( 250(login page id) ); // all your restricted pages
if ( in_array( get_queried_object_id(), $restricted ) ) {
wp_redirect( site_url( '/custom_page slug' ) );
exit();
}
}
});
I think we can use wp_redirect before content is sent to the browser. You should use hooks for that.
But you can try this for custom template:
global $current_user;
$role = $current_user->roles;
$current_role = implode( $role );
if ( $current_role == 'customrole' ) {
$url = site_url( '/role-page' );
echo "<script>window.location.href = '$url';</script>";
exit;
}

Change account menu wording if user is "logged in" in WooCommerce

I'm using WooCommerce with a child theme of Storefront.
I have a secondary menu named "Desktop secondary top right EN" where there's a link to "my account".
My question is: how to change the "my account" item wording if the customer is logged in ?
I'd like to do this without having to create another menu in the back office and without installing a plugin of course.
I should be able to use something similar to:
function menu_add_admin_buttons( $items, $args ) {
if( 'secondary' == $args['theme_location'] ) {
$btn_format = '<li>%s</li>';
if ( is_user_logged_in() ) {
$btn = sprintf($btn_format, admin_url('profile.php'), __('Your Profile') );
} else {
$btn = sprintf($btn_format, wp_login_url(), __('Log In') );
}
return $items . $btn;
}
}
$menu_filter = 'wp_nav_menu_' . sanitize_title("Desktop secondary top right EN") . '_items';
add_filter($menu_filter, 'menu_add_admin_buttons', 20, 2);
but it has to be customized to my needs. How can I do this?
You can try another wordpress hook wp_nav_menu_items:
function menu_add_admin_buttons( $items, $args ) {
$btn = '';
if ( $args->theme_location === 'secondary' ) {
$btn_format = '<li>%s</li>';
if ( is_user_logged_in() ) {
$btn = sprintf($btn_format, admin_url( 'profile.php' ), __( 'Your Profile' ) );
} else {
$btn = sprintf($btn_format, wp_login_url(), __('Log In') );
}
}
return sprintf( '%s%s', $items, $btn );
}
add_filter( 'wp_nav_menu_items','menu_add_admin_buttons', 10, 2 );
Could you please to check the documentation.
I did this instead, and it seems to work fine.
I use the standard hook : wp_setup_nav_menu_item to filter the menu items.
I check if it's not the back office (admin mode) and if woocommerce is active.
Then, if the url is the url of the "my account" menu, I make the change according to the current language.
add_filter( 'wp_setup_nav_menu_item','my_account_setup' );
function my_account_setup( $item ) {
if ( ! is_admin() && class_exists( 'woocommerce' ) ) {
if ( $item->url == esc_url( wp_login_url() ) || strpos($item->url, '/my-account-2-2/') !== false ){
if ( is_user_logged_in() ) {
if(get_locale() == 'fr_FR') {
$item->title = 'MON COMPTE';
} else {
$item->title = 'MY ACCOUNT';
}
} else {
if(get_locale() == 'fr_FR') {
$item->title = 'LOGIN';
} else {
$item->title = 'LOGIN';
}
}
}
}
return $item;
}

How to change WooCommerce My Account page titles?

I want to change the page titles (entry titles) for each page in the WooCommerce My Account section so that they relate to the actual page you are on rather than outputting a generic "My Account" on each page.
I have looked around and seen this solution in several places:
function wpb_woo_endpoint_title( $title, $id ) {
if ( is_wc_endpoint_url( 'downloads' ) && in_the_loop() ) { // add your endpoint urls
$title = "Download MP3s"; // change your entry-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 Details";
}
return $title;
}
add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );
This does not work unless you remove the in_the_loop check, which obviously isn't ideal as then it ends up changing other things on the page too.
Then I found this answer, as an example on how to change the title for the "Account Details" page:
add_filter( 'woocommerce_endpoint_edit-account_title', 'change_my_account_edit_account_title', 10, 2 );
function change_my_account_edit_account_title( $title, $endpoint ) {
$title = __( "Edit your account details", "woocommerce" );
return $title;
}
But this didn't work, it didn't even seem to go into the function at all.
Is there a way to do this that actually works?
Changing main my account page title and My account page title sub sections:
1) For "My Account: dashbord (main page):
To change the main "My account" page title, you just need to change the title of the page in backend directly because it's not an endpoint, and to check in Settings > Advanced section that the page (with the renamed title) is still assigned to "My account page".
2) For the other "endpoints" page titles in My account section:
Use woocommerce_endpoint_{$endpoint}_title composite dedicated filter hook, where {$endpoint} need to be replaced by the targeted endpoint (see available endpoints on Settings > Advanced section)
Example: To change My account "orders" endpoint page title you will use:
add_filter( 'woocommerce_endpoint_orders_title', 'change_my_account_orders_title', 10, 2 );
function change_my_account_orders_title( $title, $endpoint ) {
$title = __( "Your orders", "woocommerce" );
return $title;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If it doesn't work, it's because something else is making trouble, like your own customizations, your theme's customizations, a plugin or something else…
Related StackOverFlow answers:
Changing the titles on My Account pages in Woocommerce
Rename My account tabbed menu items in Woocommerce
Reorder menu items in Woocommerce My Account section
Display custom content for a custom account menu item in Woocommerce 3
You can change you MyAccount item titles this way:
/**
* Rename WooCommerce MyAccount menu items
*/
add_filter( 'woocommerce_account_menu_items', 'rename_menu_items' );
function rename_menu_items( $items ) {
$items['downloads'] = 'Download MP3s';
$items['orders'] = 'My Orders';
$items['edit-account'] = 'Change My Details';
return $items;
}
To change the title on each account page as well, you need to add this too:
/**
* Change page titles
*/
add_filter( 'the_title', 'custom_account_endpoint_titles' );
function custom_account_endpoint_titles( $title ) {
global $wp_query;
if ( isset( $wp_query->query_vars['downloads'] ) && in_the_loop() ) {
return 'Download MP3s';
}
if ( isset( $wp_query->query_vars['orders'] ) && in_the_loop() ) {
return 'My Orders';
}
if ( isset( $wp_query->query_vars['edit-account'] ) && in_the_loop() ) {
return 'Change My Details';
}
return $title;
}
If you're using Yoast SEO, you need to add another function to set the correct page titles in the browser tab. If you also need this, I'll expand my answer.
Put this into you functions.php file. Tested and works.
I notice I had some issues implementing the is_wc_endpoint_url() && in_the_loop solution, it did not work out with the && in_the_loop(), removing is not a viable option since it created even more conflicts.
So instead I chose to work on the template file directly, I am sharing this because I noticed that some people had the same issues and this solution might help others achieve the similar results using a different approach.
I basically replaced the header in page-my-account.php with this code:
<header>
<?php
$main_title = get_the_title();
$endpoint = WC()->query->get_current_endpoint();
$endpoint_title = __( WC()->query->get_endpoint_title( $endpoint ), 'text-domain' );
if ( $endpoint_title ){
//If the endpoint exists use itself as the new custom title
$custom_title = $endpoint_title;
}
else{
// Here we can define the custom title for each endpoint we can use home_url() or strpos + add_query_arg()
global $wp;
if( basename( home_url($wp->request) ) == 'points-and-rewards' || ( strpos( (add_query_arg( $wp->query_vars)) , 'points-and-rewards' ) !== false ) ){
$custom_title = __( 'Points and rewards', 'text-domain' );
}
elseif( basename(home_url($wp->request)) == 'payment-methods' ){
$custom_title = __( 'Payment methods', 'text-domain' );
}
elseif( basename(home_url($wp->request)) == 'my-account' ){
$custom_title = __( 'Dashboard', 'text-domain' );
}
//Add more custom titles here
}
if ( !empty( $custom_title ) ){
//If there is a custom title
$new_endpoint_title = sprintf( '<h2>%s > %s</h2>', $main_title, $custom_title );
}
else{
//If there is no custom title default to get_title()
$new_endpoint_title = sprintf( '<h2>%s</h2>' , $main_title );
}
?>
<?php //Echo's the resulting title ?>
<?php echo $new_endpoint_title; ?>
</header>

Modify simple product link in WooCommerce

I have an online store with WooCommerce where the user who is not registered as a "Subscribed Customer" and tries to add a product to the cart is redirected to the page to subscribe.
In the case of the general store where all the products appear, it works perfectly with this code:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 30, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
if ( !is_user_logged_in() || !current_user_can('tienda') ){
if( $product->is_type( 'simple' ) ){
$button_text = __( "Debes suscribirte", "woocommerce" );
$button = __('<a class="button" href="https://glancingeye.com/modelado-3d/#suscripcion-modelado">' . $button_text . '</a>', "woocommerce");
}}
return $button;
}
But once inside a product I can not achieve that if the user is not registered he will be redirected to the subscription page.
So far I have tried with this script, I modified the text of the button but not the link. This is the code for this side:
if ( !is_user_logged_in() || !current_user_can('tienda') ){
add_filter( 'woocommerce_add_to_cart_form_action', 'boton_single' );
function boton_single( $product_permalink ){
// filter...
return _e('<a class="button" href="https://glancingeye.com/modelado-3d/#suscripcion-modelado">' . $button_text . '</a>', "woocommerce");
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'texto_boton_single' );
function texto_boton_single() {
return _e( 'Debes suscribirte', 'boton_suscribete' );
}
}
What am I doing wrong?
I have made modifications to the coding.
add_filter( 'woocommerce_add_to_cart_form_action', 'boton_single' );
if ( !is_user_logged_in() || !current_user_can('tienda') ){
function boton_single( $product_permalink ){
// filter...
return _e( 'https://glancingeye.com/modelado-3d/#suscripcion-modelado', 'boton_suscribete' );
return $product_permalink;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'texto_boton_single' );
function texto_boton_single() {
return _e( 'Debes suscribirte', 'boton_suscribete' );
}
}
and now when you click on "Add to cart", the system adds the product to the cart (should not I am not a subscribed user) but in the url if the URL appears where I want it to redirect.
Once the product is added to the cart, if I try to add it again, this time it sends me to the subscription page (this should be done from the start).

Woocommerce Continue Shopping Button Link to Last Product Category Page

There are forum topics regarding custom continue shopping buttons, but there is nothing on how to link the custom button to the last product category page viewed. There is a plugin that gives the option for this; however, there is a flaw in that when the cart is refreshed, the button disappears. The following code works to create a custom message/button; however, I cannot figure out how to change the link from the shop page to the last product category page viewed:
/* Add Continue Shopping Button on Cart Page & Checkout page*/
add_action( 'woocommerce_before_cart_table',
'woo_add_continue_shopping_button_to_cart' );
add_action( 'woocommerce_before_checkout_form',
'woo_add_continue_shopping_button_to_cart' );
function woo_add_continue_shopping_button_to_cart() {
$shop_page_url = "get_permalink( wc_get_page_id( 'shop' ) )";
echo '<div class="woocommerce-message">';
echo ' <a href="'.$shop_page_url.'" class="button wc-forwards">Continue
Shopping →</a> Would you like some more prints?';
echo '</div>';
}
Thank you in advance!
J.
Here is the code that the plugin uses to link (I think):
add_action( 'woocommerce_cart_is_empty', 'hpy_cs_output_notice', 1 );
function hpy_cs_output_notice() {
$display_empty = get_option( 'hpy_cs_empty_cart_notice' );
if ( $display_empty == 'yes' ) {
$link = wc_custom_redirect_continue_shopping();
$message = sprintf('%s %s',
esc_url($link), esc_html__('Continue shopping', 'woocommerce'),
esc_html(get_option('hpy_cs_empty_cart_text', __('Your cart is empty.', '
$message = sprintf( \'%s %s\',
esc_url( $link ), esc_html__( \'Continue shopping\', \'woocommerce\' ),
esc_html( get_option( \'hpy_cs_empty_cart_text\', __( \'Your cart is
empty.\', \'hpy_cshpy_cshpy_cs\' ) ) ) );
'))));
wc_print_notice($message);
}
}
There is no built-in function to retrieve last category page viewed. But, I have some ideas on how this could be solved.
First is to create a custom action, check if page is category page, and set category ID to session data if so. Then on your button, use this to get proper URL.
/* Set up session */
add_action( 'init', 'wp_check_for_product_cat_sess');
function wp_check_for_product_cat_sess(){
if(!session_id()) {
session_start();
}
}
/* Set session variable when on Category or Product page */
add_action( 'wp', 'wp_check_for_product_cat');
function wp_check_for_product_cat(){
global $wp_query;
if( is_product_category() ){ // This is Category; use my ID
$_SESSION['wp_last_cat'] = $wp_query->get_queried_object()->term_id;
}
if( is_product() ){ // This is Product; use my ID to get my Categories
$cats = get_the_terms( $wp_query->get_queried_object(), 'product_cat' ) ;
if( count( $cats ) > 0 ){
foreach($cats as $one_cat ){
$_SESSION['wp_last_cat'] = $one_cat->term_id;
}
}
}
if( is_cart() ){
// Here we output only on Cart page, as debug tool */
var_dump( $_SESSION['wp_last_cat'] );
}
}
Then you replace in your action code:
$shop_page_url = "get_permalink( wc_get_page_id( 'shop' ) )";
Should become:
if( isset( $_SESSION['wp_last_cat'] ) ){
$shop_page_url = get_term_link( $_SESSION['wp_last_cat'], 'product_cat' );
} else {
$shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
}
Second is to retrieve last item added to cart, and use that item to retrieve its category URL.

Categories