I would like to be able to show more user details than I'm able to figure out in the woocommerce account dashboard. Details being name and email fields.
Also would like to add shipping details aswell?
My details answer
<?php
$current_user = wp_get_current_user();
/**
* #example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
* return;
*/
echo 'Email:' . $current_user->user_email . '<br />';
echo 'First Name:' . $current_user->user_firstname . '<br />';
echo 'Last Name:' . $current_user->user_lastname . '<br />';
?>
Address add to functions.php
add_action( 'woocommerce_account__shipping_dashboard', 'woocommerce_account_edit_address' );
Add to dashboard to show address
<?php do_action( 'woocommerce_account__shipping_dashboard' ); ?>
Related
The current template has the following,
<?php
/**
* Customer new account email
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-new-account.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates\Emails
* #version 6.0.0
*/
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<?php /* translators: %s: Customer username */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $user_login ) ); ?></p>
<?php /* translators: %1$s: Site title, %2$s: Username, %3$s: My account link */ ?>
<p><?php printf( esc_html__( 'Thanks for creating an account on %1$s. Your username is %2$s. You can access your account area to view orders, change your password, and more at: %3$s', 'woocommerce' ), esc_html( $blogname ), '<strong>' . esc_html( $user_login ) . '</strong>', make_clickable( esc_url( wc_get_page_permalink( 'myaccount' ) ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<?php if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && $password_generated && $set_password_url ) : ?>
<?php // If the password has not been set by the user during the sign up process, send them a link to set a new password ?>
<p><?php printf( esc_html__( 'Click here to set your new password.', 'woocommerce' ) ); ?></p>
<?php endif; ?>
<?php
/**
* Show user-defined additional content - this is set in each email's settings.
*/
if ( $additional_content ) {
echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}
do_action( 'woocommerce_email_footer', $email );
But I am in need of changing the link to another page. How do I do that.
I tried to simply change the myaccount to login where the my page is,
make_clickable( esc_url( wc_get_page_permalink( 'login' ) ) ) );
But it seems like not working.
Also, I'm required to remove the Click here to set your new password.
Please share your wisdom how I can do that. I am new to this thing. Thanks.
I'm currently adding the below code in woocommerce/cart/cart-shipping.php to change the no shipping method message for a country that is not in the shipping country list of WooCommerce.
<?php
add_filter( 'woocommerce_cart_no_shipping_available_html', 'custom_no_shipping_available_html' );
add_filter( 'woocommerce_no_shipping_available_html', 'custom_no_shipping_available_html' );
function custom_no_shipping_available_html( $message ) {
$country = WC()->customer->get_shipping_country();
if ( !empty( $country ) ) {
$all_countries = WC()->countries->get_countries();
return sprintf( "Unfortunately, we don't ship to %s. Please contact our Support if you need any help.", $all_countries[ $country ] );
}
return sprintf( "Unfortunately, we don't ship to this location. Please contact our Support if you need any help.", $all_countries[ $country ] );
}
?>
After this piece of code the default code in cart-shipping.php is:
$formatted_destination = isset($formatted_destination) ? $formatted_destination : WC()->countries->get_formatted_address($package['destination'], ', ');
$has_calculated_shipping = !empty($has_calculated_shipping);
$show_shipping_calculator = !empty($show_shipping_calculator);
$calculator_text = '';
?>
<tr class="woocommerce-shipping-totals shipping">
<th><?php echo wp_kses_post($package_name); ?></th>
<td data-title="<?php echo esc_attr($package_name); ?>">
<?php if ($available_methods) : ?>
<ul id="shipping_method" class="woocommerce-shipping-methods">
<?php foreach ($available_methods as $method) : ?>
<li>
<?php
if (1 < count($available_methods)) {
printf('<input type="radio" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d_%2$s" value="%3$s" class="shipping_method" %4$s />', $index, esc_attr(sanitize_title($method->id)), esc_attr($method->id), checked($method->id, $chosen_method, false)); // WPCS: XSS ok.
} else {
printf('<input type="hidden" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d_%2$s" value="%3$s" class="shipping_method" />', $index, esc_attr(sanitize_title($method->id)), esc_attr($method->id)); // WPCS: XSS ok.
}
printf('<label for="shipping_method_%1$s_%2$s">%3$s</label>', $index, esc_attr(sanitize_title($method->id)), wc_cart_totals_shipping_method_label($method)); // WPCS: XSS ok.
do_action('woocommerce_after_shipping_rate', $method, $index);
?>
</li>
<?php endforeach; ?>
</ul>
<?php if (is_cart()) : ?>
<p class="woocommerce-shipping-destination">
<?php
if ($formatted_destination) {
// Translators: $s shipping destination.
printf(esc_html__('Shipping to %s.', 'woocommerce') . ' ', '<strong>' . esc_html($formatted_destination) . '</strong>');
$calculator_text = esc_html__('Change address', 'woocommerce');
} else {
echo wp_kses_post(apply_filters('woocommerce_shipping_estimate_html', __('Shipping options will be updated during checkout.', 'woocommerce')));
}
?>
</p>
<?php endif; ?>
<?php
elseif (!$has_calculated_shipping || !$formatted_destination) :
if (is_cart() && 'no' === get_option('woocommerce_enable_shipping_calc')) {
echo wp_kses_post(apply_filters('woocommerce_shipping_not_enabled_on_cart_html', __('Shipping costs are calculated during checkout.', 'woocommerce')));
} else {
echo wp_kses_post(apply_filters('woocommerce_shipping_may_be_available_html', __('Enter your address to view shipping options.', 'woocommerce')));
}
elseif (!is_cart()) :
echo wp_kses_post(apply_filters('woocommerce_no_shipping_available_html', __('There are no shipping options available. Please ensure that your address has been entered correctly, or Please contact our Support if you need any help.', 'woocommerce')));
else :
// Translators: $s shipping destination.
echo wp_kses_post(apply_filters('woocommerce_cart_no_shipping_available_html', sprintf(esc_html__('No shipping options were found for %s.', 'woocommerce') . ' ', '<strong>' . esc_html($formatted_destination) . '</strong>')));
$calculator_text = esc_html__('Enter a different address', 'woocommerce');
endif;
?>
<?php if ($show_package_details) : ?>
<?php echo '<p class="woocommerce-shipping-contents"><small>' . esc_html($package_details) . '</small></p>'; ?>
<?php endif; ?>
<?php if ($show_shipping_calculator) : ?>
<?php woocommerce_shipping_calculator($calculator_text); ?>
<?php endif; ?>
</td>
</tr>
But the problem is that I have to complete all the billing address fields including phone and zip code to get my message that the selected country is not supported.
I think this happens because by default WooCommerce is checking the $formatted_destination variable set in the same PHP file.
My question is how can I have the message in a way that is directly shown once the country is selected and it is not part of the shipping country?
To answer your question, first some explanation. I copied a piece of code from templates/cart/cart-shipping.php line 66 - 72 #version 3.6.0
elseif ( ! is_cart() ) :
echo wp_kses_post( apply_filters( 'woocommerce_no_shipping_available_html', __( 'There are no shipping options available. Please ensure that your address has been entered correctly, or contact us if you need any help.', 'woocommerce' ) ) );
else :
// Translators: $s shipping destination.
echo wp_kses_post( apply_filters( 'woocommerce_cart_no_shipping_available_html', sprintf( esc_html__( 'No shipping options were found for %s.', 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>' ) ) );
$calculator_text = esc_html__( 'Enter a different address', 'woocommerce' );
endif;
As you can see this contains 2 filters hook, namely:
woocommerce_no_shipping_available_html
woocommerce_cart_no_shipping_available_html
While the second hook, in the message shows $formatted_destination, the first one doesn't.
So to answer your question, you can use the hooks like this:
The message displayed on the cart page
function filter_woocommerce_cart_no_shipping_available_html( $sprintf ) {
// Get packages
$packages = WC()->shipping()->get_packages();
// Loop through
foreach ( $packages as $i => $package ) {
$formatted_destination = WC()->countries->get_formatted_address( $package['destination'], ', ' );
}
// Message
$sprintf = sprintf( esc_html__( 'Unfortunately, we don\'t ship to %s. Please contact our Support if you need any help.', 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>' );
return $sprintf;
}
add_filter( 'woocommerce_cart_no_shipping_available_html', 'filter_woocommerce_cart_no_shipping_available_html', 10, 1 );
The message displayed on the checkout page
function filter_woocommerce_no_shipping_available_html( $html ) {
// Message
$html = __( 'My new message', 'woocommerce' )
return $html;
}
add_filter( 'woocommerce_no_shipping_available_html', 'filter_woocommerce_no_shipping_available_html', 10, 1 );
With both answers there is no need to modify/overwrite template files, code goes in functions.php file of the active child theme (or active theme).
I was new to WordPress.
Need help to display logged in user name in place of "My Account" in wordpress. Like "Hi, UserName".
Im using woocommerce plugin as my website is related to e-commerce.
Kindly help with detailed steps to solve this query.
From the WP Codex (https://codex.wordpress.org/Function_Reference/wp_get_current_user):
The call to wp_get_current_user() always returns a WP_User object.
<?php
$current_user = wp_get_current_user();
/**
* #example Safe usage:
* $current_user = wp_get_current_user();
* if ( ! $current_user->exists() ) {
* return;
* }
*/
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
?>
You might want to check if the current user is a logged in user.
<?php
// Check if user is logged in
if ( is_user_logged_in() ) {
// Get current logged in user details
$current_user = wp_get_current_user();
echo $current_user->display_name;
} else {
echo 'Not logged in';
}
?>
I have some problem in WooCommerce Admin Order menu. I want to add some content in the column but I don't know where I can make it.
This is some example screenshoot about my woocommerce admin order view :
I want to add phone number bellow email address in Order column.
Please help me to do that.
Thank you.
I've had to do this once, i've followed this tutorial :
https://code.tutsplus.com/articles/add-a-custom-column-in-posts-and-custom-post-types-admin-screen--wp-24934
add_action('manage_shop_order_posts_custom_column', 'match_order_woocommerce_custom_order_columns', 2);
function match_order_woocommerce_custom_order_columns( $column ) {
remove_action('manage_shop_order_posts_custom_column', 'woocommerce_custom_order_columns', 2);
global $post, $woocommerce;
$order = new WC_Order( $post->ID );
switch ($column) {
case "order_title" :
if ($order->user_id) $user_info = get_userdata($order->user_id);
if (isset($user_info) && $user_info) :
$user = '<a href="user-edit.php?user_id=' . esc_attr( $user_info->ID ) . '">';
if ($user_info->first_name || $user_info->last_name) $user .= $user_info->first_name.' '.$user_info->last_name;
else $user .= esc_html( $user_info->display_name );
$user .= '</a>';
else :
$user = __('Guest', 'woocommerce');
endif;
echo '<strong>'.sprintf( __('Order %s', 'woocommerce'), $order->get_order_number() ).'</strong> ' . __('made by', 'woocommerce') . ' ' . $user;
if ($order->billing_email) :
echo '<small class="meta">'.__('Email:', 'woocommerce') . ' ' . ''.esc_html( $order->billing_email ).'</small>';
endif;
if ($order->billing_phone) :
echo '<small class="meta">'.__('Tel:', 'woocommerce') . ' ' . esc_html( $order->billing_phone ) . '</small>';
endif;
break;
}
}
Please try this snippet in your active theme's functions.php
I'm writing an integration piece with a third party system and need to check certain values when a user logs in.
I've tried several different hooks: wp_init, wp_login and wp_loaded, but wp_get_current_user always returns null.
E.g.: To determine if there is a user currently logged in, do this:
<?php
wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
} else {
// Logged in.
}
?>
default usage
The call to wp_get_current_user() return WP_User object.
<?php
wp_get_current_user();
/**
* #example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
* return;
*/
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User level: ' . $current_user->user_level . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
?>
more on this page http://codex.wordpress.org/Function_Reference/wp_get_current_user
I was able to accomplish this using http://codex.wordpress.org/Function_Reference/get_user_by.
function CheckWordPressLogin($User) {
//$User contains login user name.
$CurrentUser = get_user_by('login', $User); //$CurrentUser is a WP_User object.
}
add_action('wp_login', 'CheckWordPressLogin');