Add order ID to customer sender name in WooCommerce email notification - woocommerce

How can I add an order number (order_id) to E-Mail Sender Name as follows: #order_id John Doe
For Example: "My Shop" should be changed to "#1234 John Doe".
I am using Change sender name to customer billing full name in WooCommerce email notifications answer code.
Can anyone help me?

Use the get_id() function to retrieve the order number and use sprintf() to give your string the desired format, like this:
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 = sprintf( '#%s %s', $order->get_id(), $order->get_formatted_billing_full_name() );
}
return $from_name;
}

Related

Is there a hook that precedes Woocommerce API's duplicate SKU check?

I have a site with Woocommerce and WPML + Multilingual Woocommerce installed. My problem is that I try to insert a product as a translation of a previously entered product without being aware of the ID of the main product. If I enter the ID as translation_of it works; both products share the same SKU and the translation has the SKU field disabled, which is how I want it to work. But I don't want to enter translation_of into the data that gets sent to Woocommerce. I want to only use the SKU and then let Wordpress first check if a product with that SKU already exists and replace sku with translation_of if it does.
This is how I went about it:
add_filter('woocommerce_api_create_product_data', '__create_product_data', -100, 2);
function __create_product_data($data, $api) {
if(isset($data['sku']) && $product_id = wc_get_product_id_by_sku($data['sku'])) {
$product_id = apply_filters('wpml_object_id', $product_id, 'product');
$data['translation_of'] = $product_id;
unset($data['sku']);
}
return $data;
}
But it seems to me that execution arrives at this point long after the SKU has been checked, because I noticed that I can return nothing and I still get product_invalid_sku error back. What would be the correct hook or does such a hook even exist?
My own solution:
add_filter('rest_pre_dispatch', '__rest_pre_dispatch', 10, 3);
function __rest_pre_dispatch($result, $server, $request) {
$sku = $request->get_param('sku');
if ($sku) {
$id = wc_get_product_id_by_sku($sku);
if ($id) {
$product_id = apply_filters('wpml_object_id', $id, 'product');
$request->set_param('translation_of', $product_id);
$request->offsetUnset('sku');
}
}
return $result;
}

Woocommerce - Edit account issue

My Woocommerce is setup to generate username automatically. I'm trying to use the code below to change username before save. I would like to change user name to be equal a custom field filled in billing form.
My code is:
function wc_cpf_as_username ( $user_login ) {
if( !empty($_POST['billing_cpf'] ) ) {
$user_login = $_POST['billing_cpf'];
}
elseif (!empty( $_POST['billing_cnpj'] )){
$user_login = $_POST['billing_cnpj'];
}
else{
$user_login = $_POST['billing_email'];
}
return $user_login;
}
add_filter( 'pre_user_login' , 'wc_cpf_as_username' );
The code work to create user, but this code do not work to edit user in my account page (/my-account/edit-account). Woocommerce show success message (Account details changed successfully.), but data is not changed.
I do not know what is the issue.
Could you help me?
Why you are making that complex function if you have a hook available for this. edit_user_profile_update hook i.e. located in /wp-admin/user-edit.php.
update_user_meta($user_id, 'custom_meta_key', $_POST['custom_meta_key']).
update_user_meta thats for update user meta field based on user ID.
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
update_user_meta($user_id, 'Custom_field', $_POST['your_field']);
}

Woocommerce - set role of customer by coupon

In woocommerce: I would like to group customers when they make their first order.
I installed "user role editor" plugin to create specific role (Example: student, teacher, ...)
At his first order/register, the customer has to complete the coupon field to register.
The coupon content is for example "student-coupon"
How to set the role of the customer depending of the coupon?
I tried this method:
Group Woocommerce users by Coupon
... but i couldn't make it work...
Here is my code:
// ASSIGN GROUP FROM COUPON
add_action('woocommerce_thankyou', 'coupon_group', 10, 1);
function coupon_group( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_used_coupons() as $coupon_name ){
// Retrieving the coupon ID
$coupon_post_obj = get_page_by_title($coupon_name, OBJECT, 'shop_coupon');
$coupon_id = $coupon_post_obj->ID;
$coupons_obj = new WC_Coupon($coupon_id);
if( $coupons_obj->is_type( 'student' ) ){
//get the user id of the customer from the order
$user_id = $order->get_user_id();
/*
*
* logic that adds the user to the group cricket users
* i.e. $add_user_to_coupon_group = add_user_meta($user_id, 'custom_user_group', 'cricket_users');
*
*
*/
$add_user_to_coupon_group = add_user_meta($user_id, 'student', 'student_users');
}
}
}
The coupon title is "student".
The "User Role editor" role is Student (ID student).
... Not sure if i have to change both 'custom_user_group' AND 'cricket_users' or only 'cricket_users' on the "$add_user_to_coupon_group" line
maybe somebody see a better solution?
Thank's

Can you have multiple sender email addresses - woocommerce

Is it possible to have different "FROM" / sender email addresses for the different outgoing email types in woocommerce?
For example;
Processing, Complete, Order on hold, refunded to come from orders#domain.com email address
Reset password, New accounts to come from accounts#domain.com
I can only see the option to have ALL emails sent from the same email address that is put in the "FROM" email field but would like to be able to have different FROM/sender emails for different types of email?
Late, I know... But, from what I can tell, there's no existing solution and it's certainly not an option that comes default with WooCommerce.
You can set the sender manually for each email ID you can check against with a conditional.
For example, when an email is sent, check if it is "whatever email" and if so, mail sender is "abc#abc.abc". This would be relatively straight forward with WooCommerce emails, but I'm not too sure of all the internal WP emails and whether there's something you can check against for them.
Here's how you would do it in WC (from this post) - Add this to your functions.php or a custom plugin.
// Change sender name
add_filter( 'woocommerce_email_from_name', function( $from_name, $wc_email ){
if( $wc_email->id == 'customer_processing_order' )
$from_name = 'Jack the Ripper';
return $from_name;
}, 10, 2 );
// Change sender adress
add_filter( 'woocommerce_email_from_address', function( $from_email, $wc_email ){
if( $wc_email->id == 'customer_processing_order' )
$from_email = 'jack.the.ripper#freek.com';
return $from_email;
}, 10, 2 );

Woocommerce "No Shipping methods" message: Customise message based on the Zipcode customer enters

Our store has been setup to process orders only within Sydney city. We manage this in Woocommerce by setting the allowed postcodes for a Flat Rate Delivery.
We are now extending deliveries to other other areas but only via telephone (no online orders for these new postcodes). Woocommerce displays a standard No Shipping message but we would to customise such that
Customer enters a postcode within the city, allow online orders. No
change to current behaviour.
Customer enters a postcode for which
telephone orders are allowed, show a customised message asking the
customer to make the call.
All other postcodes, disallow orders.
Any technical direction will be greatly appreciated.
Thanks.
Below code should help you. Change the value of array variable $zip_array in both the functions as a comma separated list of zip codes, which you want to show a custom message. Also, change the string value of $custom_msg to your custom message. More details, please refer this article.
// For Cart Page.
add_filter( 'woocommerce_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
// For Checkout page
add_filter( 'woocommerce_cart_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
function wf_customize_default_message( $default_msg ) {
$zip_array = array(
'30031',
);
if ( in_array( WC()->customer->get_shipping_postcode() , $zip_array) ) {
$custom_msg = "Call us for quotation - 1-800-XXX-XXXX";
if( empty( $custom_msg ) ) {
return $default_msg;
}
return $custom_msg;
}
return $default_msg;
}
add_filter('woocommerce_package_rates', 'wf_remove_shipping_options_for_particular_zip_codes', 8, 2);
function wf_remove_shipping_options_for_particular_zip_codes($rates, $package)
{
global $woocommerce;
$zip_array = array(
'30031',
);
if ( in_array( $woocommerce->customer->get_shipping_postcode() , $zip_array) ) {
$rates = array();
}
return $rates;
}

Resources