I want the billing field 'company email' to be equal to Billing E-mail address.
I thought this would work but I only get an empty field:
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 10, 1 );
function custom_admin_billing_fields( $billing_fields ) {
$billing_fields['tlr_company_email']['value'] = $billing_fields['email']['value'];
return $billing_fields;
}
I think the proper way of doing it is to update once the payment is completed, after the checkout. The following code is hooked to the payment complete trigger. It will copy the billing email to the custom company billing email.
add_action('woocommerce_payment_complete', 'replicate_billing_email', 100, 1);
function replicate_billing_email($order_id){
$order = new WC_Order($order_id); //obatin the completed order as object
$billing_email = $order->get_billing_email(); //get the billing email address
update_post_meta($order_id, 'tlr_company_email', $billing_email); //update the company email
update_post_meta($order_id, '_tlr_company_email', $billing_email); //it maybe saved like that, with underscore, ommit if not needed
}
To test it, complete a test order.
Related
I am trying to place an order without filling in the email field at the checkout form.
I used this code to remove all field values (except country) :
add_filter( 'woocommerce_checkout_get_value' , 'clear_checkout_fields' , 10, 2 );
function clear_checkout_fields( $value, $input ){
if( $input != 'billing_country' )
$value = '';
return $value;
}
When placing the order without filling in the email field the order gets placed with the email of the account it is placed with. I want to be able to place an order without filling in an email address so it shows no email address on the orders page on the WooCommerce dashboard.
Any suggestions?
Set default value for this field to admin email.
add_filter( 'woocommerce_checkout_fields', 'woo_default_email' );
function woo_default_email($fields) {
$fields['billing']['billing_email']['default'] = get_option('admin_email');
return $fields;
}
Then just remove field from output in your template and email message.
I don't need to collect customer phone numbers in woocommerce so I've used the following code to remove them from the checkout process:
add_filter( 'woocommerce_checkout_fields', 'pbj_woocommerce_checkout_fields' );
function pbj_woocommerce_checkout_fields( $fields ) {
unset($fields['billing']['billing_phone']);
unset($fields['shipping']['shipping_phone']);
$fields['billing']['billing_email']['class'] = array('form-row-wide');
return $fields;}
(sorry for hideous code pasting, first time!)
That works great on the checkout page but once in the "my account" area, the user is still required to add a phone number if they edit their billing or shipping address. I have added this code:
function pbj_remove_billingphone($fields) {
unset( $fields ['billing_phone'] );
return $fields;}
add_filter( 'woocommerce_billing_fields', 'pbj_remove_billingphone' );
which has removed it from billing, but I can't get anything to work to remove it from shipping. Any help on either something that universally removes the phone number requirement everywhere, or tips on what to filter/unset to remove the shipping phone number on the account page? Thank you!
Here is the complete way to do it in account and checkout pages (for both pages):
// Remove billing phone (and set email field class to wide)
add_filter( 'woocommerce_billing_fields', 'remove_billing_phone_field', 20, 1 );
function remove_billing_phone_field($fields) {
$fields ['billing_phone']['required'] = false; // To be sure "NOT required"
$fields['billing_email']['class'] = array('form-row-wide'); // Make the field wide
unset( $fields ['billing_phone'] ); // Remove billing phone field
return $fields;
}
// Remove shipping phone (optional)
add_filter( 'woocommerce_shipping_fields', 'remove_shipping_phone_field', 20, 1 );
function remove_shipping_phone_field($fields) {
$fields ['shipping_phone']['required'] = false; // To be sure "NOT required"
unset( $fields ['shipping_phone'] ); // Remove shipping phone field
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
Normally shipping email and phone fields doesn't exist by default in WooCommmerce
I'm looking to setup an affiliate program, starting with the email notifications like when someone applies the coupon code of a an affiliate user.
For example:
Coupon code "Bob" applied by a customer.
An email notification is sent to "bob#gmail.com" (an affiliate) about the coupon code "bob" applied by a customer in cart or checkout, before order is submitted.
I have tried to use (without any luck): $order->get_used_coupons()
Then once order is completed, an email notification will be sent again: "order was completed with exclusive code".
You can try to use a custom function hooked in woocommerce_applied_coupon action hook (fired each time a coupon is applied).
Here is just a functional example that will send an email when a coupon is applied (the email address name recipient is set with the coupon name).
You will need to customize it for your needs:
add_action( 'woocommerce_applied_coupon', 'custom_email_on_applied_coupon', 10, 1 );
function custom_email_on_applied_coupon( $coupon_code ){
if( $coupon_code == 'bob' ){
$to = "jack.hoover#gmail.com"; // Recipient
$subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
wp_mail( $to, $subject, $content );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
I using Woocommerce and actually I receive order notifications only to one email. I would like to receive notifications about orders in 2 different emails depending on customer location:
For customer from zone 1 (Germany) I would like to receive the email notifications at Mail #1 (mail1#mail.com),
For all other zones like zone 2 (Mexico) I would like to receive the email notifications at Mail #2 (mail2#mail.com).
I looking for some functions on net, but I was find only funtcions to send to two email adresses, but without any If condition.
What I will need is something like that:
if ($user->city == 'Germany') $email->send('mail1#mail.com')
else $email->send('mail2#mail.com')
Which hook can I use to get this working?
Thanks.
You can use a custom function hooked in woocommerce_email_recipient_{$this->id} filter hook, targeting 'New Order' email notification, this way:
add_filter( 'woocommerce_email_recipient_new_order', 'diff_recipients_email_notifications', 10, 2 );
function diff_recipients_email_notifications( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Set HERE your email adresses
$email_zone1 = 'name1#domain.com';
$email_zone_others = 'name2#domain.com';
// Set here your targeted country code for Zone 1
$country_zone1 = 'GE'; // Germany country code here
// User Country (We get the billing country if shipping country is not available)
$user_country = $order->shipping_country;
if(empty($user_shipping_country))
$user_country = $order->billing_country;
// Conditionaly send additional email based on billing customer city
if ( $country_zone1 == $user_country )
$recipient = $email_zone1;
else
$recipient = $email_zone_others;
return $recipient;
}
For WooCommerce 3+, some new methods are required and available from WC_Order class concerning billing country and shipping country: get_billing_country() and get_shipping_country() …
Usage with $order instance object:
$order->get_billing_country(); // instead of $order->billing_country;
$order->get_shipping_country(); // instead of $order->shipping_country;
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and works.
Related answers:
How to get order ID in woocommerce_email_headers hook
Send on-hold order status email notification to admin
I edited the Order Notes field in the checkout to be:
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Enter Purchase Order Number if applicable';
$fields['order']['order_comments']['label'] = 'Purchase Order #';
return $fields;
}
However, the confirmation and notification emails still say "Note" as the field label. I just want to change the label in the emails.
There are a whole different set of filters for the emails that are generated by woocommerce. Changing the label for the checkout field only changes the note in the checkout area! the label isn't passed through to the order emails, or to paypal for that matter.
You'll have to add another filter to override the field name in the email. Something like this:
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'My Field';
return $keys;
}