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.
Related
How do I clear a specfic checkout fields in WooCommerce after placing an order?
For example Billing email and Billing Phone. So that when the registered customer makes his/her next order, he/she has to fill that fields again?
I see this code, but this clean all the fields, i need only a specific field. Any advice?
add_filter('woocommerce_checkout_get_value','__return_empty_string',10);
The woocommerce_checkout_get_value hook has 2 arguments:
$value argument that is returned
$input argument to target checkout field(s)
So in your case, you get:
function filter_woocommerce_checkout_get_value( $value, $input ) {
// Target checkout fields. Multiple fields can be added, separated by a comma
if ( in_array( $input, array( 'billing_phone', 'billing_email' ) ) ) {
$value = '';
}
return $value;
}
add_filter( 'woocommerce_checkout_get_value' , 'filter_woocommerce_checkout_get_value' , 10, 2 );
You could probably achieve this with some custom javascript:
document.getElementById('fieldID').value = ''
Or jQuery:
$('#fieldID').val('');
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.
In Woocommerce, I would like to add a email custom field with the Advanced Custom Fields plugin to products post type.
If customer place an order I would like to add the corresponding email adresses for each order items to new order email notification .
How to add recipients from product custom fields to Woocommerce new order email notification?
For "New order" email notification, here is the way to add custom emails from items in the order (The email custom field is set with ACF in the products).
So you will set first a custom field in ACF for "product" post type:
Then you will have in backend product edit pages:
Once done and when that product custom-fields will be all set with an email adress, you will use this code that will add to "New order" notification the emails for each corresponding item in the order:
add_filter( 'woocommerce_email_recipient_new_order', 'add_item_email_to_recipient', 10, 2 );
function add_item_email_to_recipient( $recipient, $order ) {
if( is_admin() ) return $recipient;
$emails = array();
// Loop though Order IDs
foreach( $order->get_items() as $item_id => $item ){
// Get the student email
$email = get_field( 'product_email', $item->get_product_id() );
if( ! empty($email) )
$emails[] = $email; // Add email to the array
}
// If any student email exist we add it
if( count($emails) > 0 ){
// Remove duplicates (if there is any)
$emails = array_unique($emails);
// Add the emails to existing recipients
$recipient .= ',' . implode( ',', $emails );
}
return $recipient;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related: Send Woocommerce Order to email address listed on product page
I'm using Woocommerce and the Product Addons plugin to add extra fields to a product. One of those fields is an email address to let people send the confirmation of the order to a DIFFERENT address than the billing address shown on the checkout page. Emails should be sent to both addresses.
Any thoughts on maybe how to modify the functions.php file to do this?
In the woocommerce_email_recipient_{$this->id} filter hook, you can use the $order argument to get your 2nd email.
But first Lets add globally an email field with the Product Add-ons plugin…
The add on field on the product (fill the field and add to cart):
This "Email" field in order-received (Thank you) page, after checkout:
As you can notice the label of this field is "Email"…
Now if I look in the database in wp_woocommerce_order_itemmeta for this order I can see for the meta_key "Email" the meta_value "loic#TheAztec.com" :
Now I can set the correct meta_key in the code below to get my email.
Here is the code that will add this additional email recipient for processing and completed customer order email notifications:
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'additional_customer_email_recipient', 10, 2 ); // Processing Order
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'additional_customer_email_recipient', 10, 2 ); // Completed Order
function additional_customer_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
$additional_recipients = array(); // Initializing…
// Iterating though each order item
foreach( $order->get_items() as $item_id => $item_data ){
// HERE set the the correct meta_key (like 'Email') to get the correct value
$email = wc_get_order_item_meta( $item_id, 'Email', true );
// Avoiding duplicates (if many items with many emails)
// or an existing email in the recipient
if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
$additional_recipients[] = $email;
}
// Convert the array in a coma separated string
$additional_recipients = implode( ',', $additional_recipients);
// If an additional recipient exist, we add it
if( count($additional_recipients) > 0)
$recipient .= ','.$additional_recipients;
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
You can add below code in your function.php
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);
function your_email_recipient_filter_function($recipient, $object) {
$recipient = $recipient . ', me#myemail.com';
return $recipient;
}
and if you want to send email in BCC then please try below code:
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
if ($object == 'customer_completed_order') {
$headers .= 'BCC: My name <my#email.com>' . "\r\n";
}
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;
}