I'm trying to add custom meta to my new order emails. At this moment, custom meta shows up when I use the "Resend new order notification", but it doesn't when a purchase is made through the website.
I suspect this has to do with my code not being able to check the Order Details before sending the email notification. Here's the code I came up with so far..
// Ref: https://www.businessbloomer.com/woocommerce-add-extra-content-order-email/
function custom_order_type_number_email( $order, $sent_to_admin, $plain_text, $email ) {
// Get custom Order Type and Order Number meta keys
$custom_order_type = get_post_meta( $order->get_id(), 'custom_order_type', true );
$custom_order_number = get_post_meta( $order->get_id(), 'custom_order_number', true );
// Add for New Order email notification only
if ( $email->id == 'new_order' ) {
if ( !empty( $custom_order_type ) ) {
echo '<p><b>Order type :</b> ' . $custom_order_type . '</p>';
} else {
echo '<p><b>Order type :</b> Website</p>';
}
if ( !empty( $custom_order_number ) ) {
echo '<p><b>Order no :</b> ' . $custom_order_number . '</p>';
} else {
echo '<p><b>Order no :</b> New</p>';
}
}
}
add_action( 'woocommerce_email_after_order_table', 'custom_order_type_number_email', 20, 4 );
What I'm trying to do ideally is..
When the customer purchases from the website, this new order will be without the custom meta. So the custom meta in the email will show Order type: Website / Order number: New.
And if the admin manually adds a new order with defined custom meta, this new order email notification would get and show the saved custom meta later.
I hope this made sense. (>_<) And thanks in advance for the help.
Related
Hello I have a woo commerce website and I am selling some books every thing is cleared but I need create custom order tracking functionality in woo commerce code to add order tracking functionality for end user is it possible if possible how can I do this please help me.
I create a custom page name as woocommerce-custom-order-tracking.php
and code is given below
<?php
// Register a custom endpoint to handle order tracking
function wc_register_order_tracking_endpoint() {
add_rewrite_endpoint( 'order-tracking', EP_PAGES );
}
add_action( 'init', 'wc_register_order_tracking_endpoint' );
// Display the order tracking form
function wc_display_order_tracking_form() {
if ( ! is_wc_endpoint_url( 'order-tracking' ) ) {
return;
}
// Get the order id from the query string
$order_id = absint( $_GET['order_id'] );
// Get the order
$order = wc_get_order( $order_id );
if ( $order ) {
// Display the order tracking information
echo '<p>Order Number: ' . $order->get_order_number() . '</p>';
echo '<p>Order Status: ' . wc_get_order_status_name( $order->get_status() ) . '</p>';
echo '<p>Tracking Number: ' . get_post_meta( $order_id, '_tracking_number', true ) . '</p>';
} else {
echo '<p>Invalid order ID.</p>';
}
}
add_action( 'woocommerce_before_single_product', 'wc_display_order_tracking_form' );
I have a holiday letting wordpress site which has the advanced custom field "Supplier Email" in the add listing page. When a rental is booked the Supplier Email needs to be added to the woocommerce New Order email under BCC, i have tried numerous options but they have not worked, the Supplier Email is in the post meta so it needs to be pulled in to the new order email. This is the code i have in my functions.php file:
add_filter( 'woocommerce_email_headers', 'bcc_to_email_headers', 10, 3 );
function bcc_to_email_headers( $headers, $email_id, $order ) {
if ( $email_id === 'new_order' ) {
$supplier_email = get_field( 'supplier_email_main',$post_id);
if ( $supplier_email ) {
$headers .= "CC: Supplier <" . $supplier_email . ">\r\n";
$headers .= "BCC: New Order <myemail#gmail.com>" . "\r\n";
}
}
return $headers;
}
The gmail address(this would go to my personal email, have hidden it here for obvious reasons) i added to BCC didnt even get sent. Not sure how to proceed with this, any help would be greatly appreciated, please note i am not a wordpress developer. Thanks in advance.
try this
add_filter( 'woocommerce_email_headers', 'add_bcc_header', 10, 3 );
function add_bcc_header( $headers, $id, $order ) {
if ( $id == 'new_order' ) {
$headers .= 'Bcc: gmailaddress#gmail.com' . "\r\n";
}
return $headers;
}
This question already has answers here:
Customizing email headers in WooCommerce email notifications
(1 answer)
How to add Custom Field as a CC to WooCommerce Customer Order Email
(1 answer)
Woocommerce: Adding second email address not working, unless recipient is admin
(2 answers)
Closed 3 years ago.
I have a custom field that contains an additional customer email account.
My idea is that when an order changes state WAIT, PENDING, PROCESSING or COMPLETED, it will reach the email of the one configured in this field.
Is this possible?
There is no problem that requires programming, but I don't know what hook to use.
Thank you.
Add the follows code snippet in your active theme's functions.php -
function add_cc_to_wc_order_emails( $header, $mail_id, $mail_obj ) {
$available_for = array( 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order' );
if( in_array( $mail_id, $available_for ) ) {
$cc_email = 'addyour#ccmail.com';
$cc_username = "yourCCUser";
$formatted_email = utf8_decode( $cc_username . ' <' . $cc_email . '>' );
$header .= 'Cc: ' . $formatted_email . "\r\n";
}
return $header;
}
add_filter( 'woocommerce_email_headers', 'add_cc_to_wc_order_emails', 99, 3 );
This might be quite useful:
https://www.skyverge.com/blog/add-woocommerce-email-recipients-conditionally/
I would take a look at the Considerations section.
You could use woocommerce_order_status_changed hook for example to notify someone each time an order change of status, like in this example:
add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );
function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){
$to_email = 'john.doe#gmail.com'; // <= Replace with your email custom field (the recipient)
$shop_name = __('Shop name'); // Set the shop name
$admin_email = 'shop#email.com'; // Set default admin email
$subject = sprintf( __('Order %s has changed to "%s" status'), $order_id, $new_status ); // The subject
$message = sprintf( __('Order %s has changed to "%s" status'), $order_id, $new_status ); // Message content
$headers = sprintf( __('From: %s <%s>'), $shop_name, $admin_email ) . "\r\n"; // From admin email
wp_mail( $to_email, $subject, $message, $headers ); // Send the email
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works
on my baked goods site, I have WooCommerce installed and another plugin called Order Delivery Date for WooCommerce.
I installed the second plugin so my customers would be able to choose a delivery date for their items, however, I am trying to make the form field a required field. So far, I've just been able to make the field look like a required field, but have not figured out how to make sure that it is actually enforced. Any ideas?
Also, if anyone is familiar with WooCommerce, do you know how I would be able to make it so that customers receive this delivery date information in their order confirmation emails?
Thank you in advance!
My site: www.monpetitfour.com
You should try to had something like that :
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// You can make your own control here
if ( ! $_POST[ 'e_deliverydate' ] )
wc_add_notice( __( 'Please select a delivery date' ), 'error' );
}
For the email, the easiest is to save the meta value ( I think it's already done by your plugin). Then you need to copy the template email (customer-processing-order.php) on your theme and change in the template :
<?php $delivery_date = get_post_meta( $order->id, 'custom_field_date', true);
// If the plugin is well developed, you can't directly use magic getters :
// $delivery_date = $order->e_deliverydate;
// Can only by use if the post meta start with _
?>
Your delivery date is <?php echo $delivery_date ?>
You can also use
date_i18n( woocommerce_date_format(), strtotime( $delivery_date ) );
In order to format the date correctly.
On the code above, you just need to find the name of the custom field used by the plugin ( you can search easily on the table wp_postmeta searching by an existing order (should be _e_deliverydate).
Add the following code to your theme's functions.php file
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['e_deliverydate'] )
wc_add_notice( __( 'Please select a delivery date.' ), 'error' );
}
Now to get the email to show the custom field,
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['Delivery Date'] = '_e_deliverydate';
return $keys;
}
EDIT : Seems the field value isn't being saved to the database, try saving it explicitly
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['e_deliverydate'] ) ) {
update_post_meta( $order_id, '_e_deliverydate', sanitize_text_field( $_POST['e_deliverydate'] ) );
}
}
I've created a meta box. The code is:
// Create your custom meta box
add_action( 'add_meta_boxes', 'hotel_amenities' );
// Add a custom meta box to a post
function hotel_amenities( $post ) {
add_meta_box(
'Meta Box Amenities', // ID, should be a string
'Amenities', // Meta Box Title
'amenities_content', // Your call back function, this is where your form field will go
'post', // The post type you want this to show up on, can be post, page, or custom post type
'normal', // The placement of your meta box, can be normal or side
'high' // The priority in which this will be displayed
);
}
// Content for the custom meta box
function amenities_content( $post ) {
echo '<label>Bed room</label>';
echo '<input type="text" name="amenity_bed_room" value="" />';
}
// Save your meta box content
add_action( 'save_post', 'save_amenities' );
// save newsletter content
function save_amenities(){
global $post;
// Get our form field
if( $_POST ) :
$amenities_meta = esc_attr( $_POST['amenity_bed_room'] );
// Update post meta
update_post_meta($post->ID, '_amenities_custom_meta', $amenities_meta);
endif;
}
It shows a meta box on admin post page with a text field. but it gets blank if I save or update the post after I put some thing on the text field.
Seems function save_amenities() is not working. What I am doing wrong in this code?
Also for getting that value I use the function below. Is that correct?
//get amenities meta box values
function get_amenities_meta_box() {
global $post;
$meta_values = get_post_meta($post->ID, '_amenities_custom_meta', true);
}
There are a few things going wrong there. The final value that you want to see will be displayed by the value attribute in the amenities_content function. Right now it is just displaying an empty string (""). Try putting any value in that attribute and you should see it show up in the meta box (value="this is a test").
The save_amenities function should take $post_id as a parameter. You'll need that to update the post meta-data and give a real value for the amenities_content function to echo back to the admin screen.
The amenities_content function should really have a nonce field that should then be verified by the save_amenities function. And user input should be sanitized before it is saved (I'm doing it both when I save it and when I display it. I'm not sure if that's necessary.)
try this out for the amenities_content function:
function amenities_content( $post ) {
// This is the value that was saved in the save_amenities function
$bed_room = get_post_meta( $post->ID, '_amenity_bed_room', true );
wp_nonce_field( 'save_amenity', 'amenity_nonce' );
echo '<label>Bed room</label>';
echo '<input type="text" name="amenity_bed_room"
value="' . sanitize_text_field( $bed_room ) . '" />';
}
and this for the save_amenities function:
function save_amenities( $post_id ) {
// Check if nonce is set
if ( ! isset( $_POST['amenity_nonce'] ) ) {
return $post_id;
}
if ( ! wp_verify_nonce( $_POST['amenity_nonce'], 'save_amenity' ) ) {
return $post_id;
}
// Check that the logged in user has permission to edit this post
if ( ! current_user_can( 'edit_post' ) ) {
return $post_id;
}
$bed_room = sanitize_text_field( $_POST['amenity_bed_room'] );
update_post_meta( $post_id, '_amenity_bed_room', $bed_room );
}