Adding custom field in WooCommerce new user registration email - php

I wrote a custom user registration form that handles login and registration for my WordPress Woocommerce web site.
When a user registers via my custom form handler I triggered Woocommerce to send the new user an email. But I need to add an activation code on the email.
Is it possible to do so?
Thanks

First you need to register this activation code in the user metadata.
For example using in your code something like:
// Set your activation code in the user meta
$activation_code = 'dHu12548-oh$r' // example for a generated activation code
// Saving the activation code in user meta data.
update_user_meta( $user_id, 'activation_code', $activation_code );
Then you can use a custom function hooked in woocommerce_email_header action hook:
add_action( 'woocommerce_email_header', 'custom_email_new_account', 100, 2 );
function custom_email_new_account( $email_heading, $email ) {
if ( 'customer_new_account' === $email->id ){
$user_id = $email->object->ID;
$activation_code = get_user_meta( $user_id, 'activation_code', $true );
// Displaying the activation code
printf( __( 'Here is your activation code: %s', 'woocommerce' ), '<strong>' . esc_html( $activation_code ) . '</strong>' );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Or you can insert in the WooCommerce template customer-new-account.php this similar code:
<?php
if ( 'customer_new_account' === $email->id ){
$user_id = $email->object->ID;
$activation_code = get_user_meta( $user_id, 'activation_code', $true );
// Displaying the activation code
printf( __( 'Here is your activation code: %s', 'woocommerce' ), '<strong>' . esc_html( $activation_code ) . '</strong>' );
}
?>

Related

Change sender name to customer billing full name in WooCommerce email notifications

How can I change E-Mail Sender Name into customer “Billing First & Last Name” using woocommerce_email_from_name hook?
For Example: "My Shop" should be changed to "John Doe".
Based on Change sender name and email address for specific WooCommerce email notifications answer code, here is my function:
add_filter( ‘woocommerce_email_from_name’, function( $from_name, $wc_email ){
if( $wc_email->id == ‘customer_processing_order’ )
// $from_name = ‘Jack the Ripper’;
$from_name = get_user_meta( $user_id, ‘first_name’, true, $user_id, ‘last_name’, true );
//$from_name = get_user_meta( $user_id, ‘first_name’, true );
return $from_name;
}, 10, 2 );
ut it doesn't work. Can anyone help me?
The following will change the "From name" to customer billing full name on Woocommerce email notifications:
add_filter( 'woocommerce_email_from_name', 'filter_wc_email_from_name', 10, 2 );
function filter_wc_email_from_name( $from_name, $email ){
if( is_a($email->object, 'WC_Order') ) {
$order = $email->object;
$from_name = $order->get_formatted_billing_full_name();
}
return $from_name;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Addition: For "new order email notification, you will use:
add_filter( 'woocommerce_email_from_name', 'filter_wc_email_from_name', 10, 2 );
function filter_wc_email_from_name( $from_name, $email ){
if( $email->id == 'new_order' && is_a($email->object, 'WC_Order') ) {
$order = $email->object;
$from_name = $order->get_formatted_billing_full_name();
}
return $from_name;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Related: Change sender name and email address for specific WooCommerce email notifications

Woocommerce Login: If user account does not exist, display error

Currently when a user attempts to login with an email/username which does not exist, this default woocommerce error is displayed:
I am trying check: If email/username DOES NOT exist, display different message.
e.g. "No account exists with this email, please create one."
The hook woocommerce_registration_error_email_exists checks if a user tried to register with an email which already exists, so I am trying to reverse this with !email_exists - Can this be applied to the login field?
My code below is not triggering and still displays the default message:
add_filter( 'woocommerce_registration_error_email_exists', 'no_account_found' );
function no_account_found($email, $username = '', $password = '' ){
if ( !email_exists( $email ) ) {
return new WP_Error( 'registration-error-email-exists', apply_filters( 'woocommerce_registration_error_email_exists', __( 'No account found with this email. Please create one.', 'woocommerce' ), $email ) );
}
}
email_exists( string $email ) returns the user's ID on success, and false on failure.
So try change your condition like this :
<?php
add_filter( 'woocommerce_registration_error_email_exists', 'no_account_found' );
function no_account_found($email, $username = '', $password = '' ){
if (email_exists($email) ) {
//the email exists do something
}else{
//email does not exist
return new WP_Error( 'registration-error-email-exists', apply_filters( 'woocommerce_registration_error_email_exists', __( 'No account found with this email. Please create one.', 'woocommerce' ), $email ) );
}
}

Targetting WooCommerce customer processing and completed order email notifications

I'm trying to add custom content after the order table on the Woocommerce
"Processing Order" and "Order Completed" customer emails, using the following code.
It should only be added if the customer chose "Local Pickup" as the shipping method.
function local_pickup_extra_content_email($order, $is_admin_email) {
if ( $is_admin_email ) {
return;
}
if ( ICL_LANGUAGE_CODE == "he" && strpos( $order->get_shipping_method(), 'Local Pickup' ) !== false) {
echo '<p><strong>Note:</strong> Please wait for telephone confirmation of local pickup.</p>';
}
}
add_action( 'woocommerce_email_after_order_table', 'local_pickup_extra_content_email', 10, 2 );
The content isn't being added to the emails specified. It's only being added to the "Order Details/Invoice" email that's sent manually through the Woocommerce order admin page.
How can I add the above content to the emails mentioned? What am I doing wrong?
(The email templates aren't overridden in the theme folder)
This can be done easily targeting those email notifications through the missing hook argument $email, this way:
add_action( 'woocommerce_email_after_order_table', 'local_pickup_extra_content_email', 10, 4 );
function local_pickup_extra_content_email( $order, $sent_to_admin, $plain_text, $email ) {
// Only for "Processing Order" and "Order Completed" customer emails
if( ! ( 'customer_processing_order' == $email->id || 'customer_completed_order' == $email->id ) ) return;
$lang = get_post_meta( $order->id, 'wpml_language', true );
if ( $lang == 'he' && && strpos( $order->get_shipping_method(), 'Local Pickup' ) !== false) {
echo '<p><strong>Note:</strong> Please wait for telephone confirmation of local pickup.</p>';
}
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works
Similar answer: Add a custom text to specific email notification for local pickup Woocommerce orders
This didn't work because of my WPML condition in the if statement:
ICL_LANGUAGE_CODE == "he"
I don't think ICL_LANGUAGE_CODE exists when Woocommerce sends an email. To fix this, I replaced the if statement in the question above with with the following, and it worked like a charm:
$lang = get_post_meta( $order->id, 'wpml_language', true );
if ( $lang == 'he' && && strpos( $order->get_shipping_method(), 'Local Pickup' ) !== false) {
echo '<p><strong>Note:</strong> Please wait for telephone confirmation of local pickup.</p>';
}

How to login with only email and password in WooCommerce

Plugin Name: WooCommerce
plugins\woocommerce\templates\global\form-login.php
<label for="username"><?php _e( 'Email', 'woocommerce' ); ?> <span class="required">*</span></label>
It display email instead of username or email which i wanted
includes\class-wc-form-handler.php
if ( empty( $username ) ) {
throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . __( 'Email is required.', 'woocommerce' ) );
}
It display validation for email correct but it login with username also
I want it to login only with email and password before checkout
If you look closely at
wp-content/plugins/woocommerce/includes/class-wc-form-handler.php at
line num 878 there is a filter
woocommerce_process_login_errors, which accepts validations
error, username and password. So you can overwrite this by this filter
help.
Here is the code:
// define the woocommerce_process_login_errors callback
function filter_woocommerce_process_login_errors($validation_error, $post_username, $post_password)
{
//if (strpos($post_username, '#') == FALSE)
if (!filter_var($post_username, FILTER_VALIDATE_EMAIL)) //<--recommend option
{
throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'Please Enter a Valid Email ID.', 'woocommerce' ) );
}
return $validation_error;
}
// add the filter
add_filter('woocommerce_process_login_errors', 'filter_woocommerce_process_login_errors', 10, 3);
Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works. in WooCommerce 2.6.X
Hope this helps!

How can I change the header of WordPress 'retrieve_password' email?

I noticed WordPress give us some filters to customize the password reset request email like wp_mail_from, retrieve_password_title and retrieve_password_message and these last two are executed in the retrieve_password function at wp-login.php as following:
$title = apply_filters( 'retrieve_password_title', $title );
$message = apply_filters( 'retrieve_password_message', $message, $key );
if ( $message && !wp_mail( $user_email, wp_specialchars_decode( $title ), $message ) )
wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function.') );
return true;
That's cool but I also realized the wp_mail function does not use any header which make me believe everything should be override with filters.
Is there a filter to override the headers? Specifically Content-Type? And where would be the best place to register them?
To run the filter wp_mail_content_type only in wp-login.php page, we can use one of its action hooks. Untested:
add_action( 'login_form_retrievepassword', function()
{
add_filter( 'wp_mail_content_type', function( $old_content_type )
{
$new_content_type = 'text/plain';
return $new_content_type;
});
});

Categories