Save WooCommerce custom Session Variables as order meta data - php

I'm trying to store custom session variables in database. Then, show them inside new order email and order details in WooCommerce Admin.
I have custom variables inside session:
add_action( 'init', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
// Early initialize customer session
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
if ( isset( $_GET['konumu'] ) || isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
Firstly, I added custom variables to database with this code;
// Storing session variables for using them in order notifications
add_action( 'woocommerce_checkout_create_order', 'oturum_degiskeni_kaydet' );
function oturum_degiskeni_kaydet( $order, $data ) {
if ( $_POST['konumu'] ) update_meta_data( $order_id, '_konum', esc_attr( $_POST['konumu'] ) );
if ( $_POST['masa_no'] ) update_meta_data( $order_id, '_masa', esc_attr( $_POST['masa_no'] ) );
}
Second, I added this variable's data to a new order email for admin.
// Show this session variables in new order email for admin
add_action( 'woocommerce_email_after_order_table', 'konumu_emaile_ekle', 20, 4 );
function konumu_emaile_ekle( $order, $sent_to_admin, $plain_text, $email ) {
if ( get_post_meta( $order->get_id(), '_konum', true ) ) echo '<p><strong>Konum :</strong> ' . get_post_meta( $order->get_id(), '_konum', true ) . '</p>';
if ( get_post_meta( $order->get_id(), '_masa', true ) ) echo '<p><strong>Masa Numarası :</strong> ' . get_post_meta( $order->get_id(), '_masa', true ) . '</p>';
}
Last part of the code is shown session variables data in WooCommerce order page;
// Show session variable in woocommerce order page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'konumu_admine_ekle', 10, 1 );
function konumu_admine_ekle( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_konum', true ) ) echo '<p><strong>Konum :</strong> ' . get_post_meta( $order_id, '_konum', true ) . '</p>';
if ( get_post_meta( $order_id, '_masa', true ) ) echo '<p><strong>Masa Numarası :</strong> ' . get_post_meta( $order_id, '_masa', true ) . '</p>';
}
But, it does not work. When customer made an order, it gave an error "We were unable to process your order, please try again."

Updated: There are some mistakes in your code when you are trying to save your custom data from session as order meta data and display it on emails and admin order pages…
Your first function is correct (oturum_degiskeni_olustur)…
Assuming that data is passed through URL like: website.com/?konumu=newyork&masa_no=12
Here is the revisited code:
// Unchanged
add_action( 'init', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
// Early initialize customer session
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
if ( isset( $_GET['konumu'] ) || isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
// Save custom session data as order meta data
add_action( 'woocommerce_checkout_create_order', 'oturum_degiskeni_kaydet' );
function oturum_degiskeni_kaydet( $order ) {
$data = WC()->session->get( 'custom_data' ); // Get custom data from session
if ( isset($data['konum']) ) {
$order->update_meta_data( '_konum', $data['konum'] );
}
if ( isset($data['masa']) ) {
$order->update_meta_data( '_masa', $data['masa'] );
}
WC()->session->__unset( 'custom_data' ); // Remove session variable
}
// Show this session variables in new order email for admin and in woocommerce order page
add_action( 'woocommerce_email_after_order_table', 'konumu_emaile_admine_ekle', 20 );
add_action( 'woocommerce_admin_order_data_after_billing_address', 'konumu_emaile_admine_ekle' );
function konumu_emaile_admine_ekle( $order ) {
if ( $konum = $order->get_meta( '_konum' ) )
echo '<p><strong>Masa Numarası :</strong> ' . $konum . '</p>';
if ( $masa = $order->get_meta( '_masa' ) )
echo '<p><strong>Konum :</strong> ' . $masa . '</p>';
}
Code goes in functions.php file of your active child theme (or active theme). Tested and Works.
On the email notification before customer details:
On admin order edit page (under billing phone):
Tested on WooCommerce 4.2+ under storefront theme

Related

Target specific WooCommerce email notifications when using the "woocommerce_order_item_meta_start" hook

I have this function which adds custom meta field to the product detail in all WooCommerce emails. But I need to show only after the order is paid (this can be also just the "completed" email).
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
if ( ! empty($ot_address) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address );
}
}
}
I hoped that I can nest it inside if ( $email->id == 'customer_completed_order' ) {}, so the final code will look like this:
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
if ( $email->id == 'customer_completed_order' ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
if ( ! empty($ot_address) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address );
}
}
}
}
But it stops working after that change. Any advice?
As you can see in your code attempt, $email is not part of the woocommerce_order_item_meta_start hook. So to target certain WooCommerce email notifications, you will need a workaround.
Step 1) creating and adding a global variable, via another hook that only applies to WooCommerce email notifications.
// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_id'] = $email->id;
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
Step 2) In the hook woocommerce_order_item_meta_start, use the global variable so we can target certain WooCommerce email notifications
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
// Getting the email ID global variable
$ref_name_globals_var = isset( $GLOBALS ) ? $GLOBALS : '';
$email_id = isset( $ref_name_globals_var['email_id'] ) ? $ref_name_globals_var['email_id'] : '';
// NOT empty and targeting specific email. Multiple statuses can be added, separated by a comma
if ( ! empty ( $email_id ) && in_array( $email_id, array( 'new_order', 'customer_completed_order' ) ) ) {
// Get meta
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
// OR use to get meta
// $ot_address = $item->get_meta( 'ot_address' );
if ( ! empty( $ot_address ) ) {
printf( '<div>' . __( 'Terms: %s', 'woocommerce' ) . '</div>', $ot_address );
}
}
}
}
add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );

Metabox with multiple custom fields for WooCommerce admin order pages

I found the below code the other day, and now my client wants two more fields. If I add it again to my snippet plugin it says “Cannot redeclare function set_custom_edit_shop_order_columns.”. Could someone kindly help me to change the code with two more custom fields?
I have changed custom_column to courier_reference and want another text field tracking_number and one more is shipping_date with a date picker rather than a text box.
Any help is appreciated.
//from::https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
// For displaying in columns.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['custom_column'] = __( 'Custom Column', 'your_text_domain' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'custom_column' :
echo esc_html( get_post_meta( $post_id, 'custom_column', true ) );
break;
}
}
// For display and saving in order details page.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_column',
__( 'Custom Column', 'your_text_domain' ),
'shop_order_display_callback',
'shop_order'
);
}
// For displaying.
function shop_order_display_callback( $post ) {
$value = get_post_meta( $post->ID, 'custom_column', true );
echo '<textarea style="width:100%" id="custom_column" name="custom_column">' . esc_attr( $value ) . '</textarea>';
}
// For saving.
function save_shop_order_meta_box_data( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
}
// Make sure that it is set.
if ( ! isset( $_POST['custom_column'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['custom_column'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_column', $my_data );
}
add_action( 'save_post', 'save_shop_order_meta_box_data' );
Updated... You just need to have multiple different slugs in the same functions. For your desired 3 custom fields use the following:
// Add columns to admin orders table.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['courier_reference'] = __( 'Courier reference', 'woocommerce' );
$columns['tracking_number'] = __( 'Tracking number', 'woocommerce' );
$columns['shipping_date'] = __( 'Shipping date', 'woocommerce' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'courier_reference' :
echo esc_html( get_post_meta( $post_id, 'courier_reference', true ) );
break;
case 'tracking_number' :
echo esc_html( get_post_meta( $post_id, 'tracking_number', true ) );
break;
case 'shipping_date' :
echo esc_html( get_post_meta( $post_id, 'shipping_date', true ) );
break;
}
}
// Add a metabox.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_meta_box',
__( 'Tracking information', 'woocommerce' ),
'shop_order_content_callback',
'shop_order'
);
}
// For displaying metabox content
function shop_order_content_callback( $post ) {
// Textarea Field
$courier_reference = get_post_meta( $post->ID, 'courier_reference', true );
echo '<p>' . __( 'Courier reference', 'woocommerce' ) . '<br>
<textarea style="width:100%" id="courier_reference" name="courier_reference">' . esc_attr( $courier_reference ) . '</textarea></p>';
// Text field
$tracking_number = get_post_meta( $post->ID, 'tracking_number', true );
echo '<p>' . __( 'Tracking number', 'woocommerce' ) . '<br>
<input type="text" style="width:100%" id="tracking_number" name="tracking_number" value="' . esc_attr( $tracking_number ) . '"></p>';
// Date picker field
$shipping_date = get_post_meta( $post->ID, 'shipping_date', true );
echo '<p>' . __( 'shipping_date', 'woocommerce' ) . '<br>
<input type="date" style="width:100%" id="shipping_date" name="shipping_date" value="' . esc_attr( $shipping_date ) . '"></p>';
}
// For saving the metabox data.
add_action( 'save_post_shop_order', 'save_shop_order_meta_box_data' );
function save_shop_order_meta_box_data( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
// Make sure that 'shipping_date' is set.
if ( isset( $_POST['courier_reference'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'courier_reference', sanitize_textarea_field( $_POST['courier_reference'] ) );
}
// Make sure that 'tracking_number' it is set.
if ( isset( $_POST['tracking_number'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'tracking_number', sanitize_text_field( $_POST['tracking_number'] ) );
}
// Make sure that 'shipping_date' is set.
if ( isset( $_POST['shipping_date'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'shipping_date', sanitize_text_field( $_POST['shipping_date'] ) );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Woocommerce change order recieved page title

I am trying to change/add in the title of the "Order Recieved" Woocommerce page.
The below snippet works - I am able to change the pre-existing TEXT with the following code:
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = $str . ' We have emailed the purchase receipt to you.';
return $new_str;
}
The below snippet does not work. - I am unable to change/add the TITLE and also pass in the username to personalise it. Here is the code and also an image of the output I am trying to achieve....The "You are awesome FIRSTNAME" added in.
<?php
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
This should be possible as there are examples on how to do it, such as here...I just can't figure out why the main title wont even appear?
As a workaround I inspected the CSS and changed the text below the header to be a larger size and the font family required.
Then through the below PHP I created the custom text with the customer name in the header.
add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
$new_str = sprintf( "You are awesome, %s :) - We've recieved your order.", esc_html( $order->get_billing_first_name() ) );
return $new_str;
}
2022 update:
function ps_title_order_received( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
unset( $order );
}
}
if ( isset ( $order ) ) {
$title = sprintf( "Thank you, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
add_filter( 'the_title', 'ps_title_order_received', 10, 2 );

Customize Order received page based on shipping method in WooCommerce

How could I customise the order thank you page based on the order's shipping method? So for example if a customer used 'Delivery on request' option, the thank you page would display a different title.
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key ) {
$order = false;
}
}
if ( isset ( $order ) ) {
$chosen_titles = array();
$available_methods = $wp->shipping->get_packages();
$chosen_rates = ( isset( $wp->session ) ) ? $wp->session->get( 'chosen_shipping_methods' ) : array();
foreach ($available_methods as $method)
foreach ($chosen_rates as $chosen) {
if( isset( $method['rates'][$chosen] ) ) $chosen_titles[] = $method['rates'][ $chosen ]->label;
}
if( in_array( 'Delivery price on request', $chosen_titles ) ) {
//$title = sprintf( "You are awesome, %s!", esc_html( $order->billing_first_name ) ); // use this for WooCommerce versions older then v2.7
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
}
return $title;
}
is_order_received_page() doesn't exist. Instead use is_wc_endpoint_url( 'order-received' )…
Also $wp->session or $wp->shipping will not work. Instead you can find the chosen shipping method data in the order item "shipping".
Try this instead:
add_filter( 'the_title', 'customizing_order_received_title', 10, 2 );
function customizing_order_received_title( $title, $post_id ) {
if ( is_wc_endpoint_url( 'order-received' ) && get_the_ID() === $post_id ) {
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
$order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
if ( empty($order_id) || $order_id == 0 )
return $title; // Exit
$order = wc_get_order( $order_id );
if ( $order->get_order_key() != $order_key )
return $title; // Exit
$method_title_names = array();
// Loop through Order shipping items data and get the method title
foreach ($order->get_items('shipping') as $shipping_method )
$method_title_names[] = $shipping_method->get_name();
if( in_array( 'Delivery price on request', $method_title_names ) ) {
$title = sprintf( "You are awesome, %s!", esc_html( $order->get_billing_first_name() ) );
}
}
return $title;
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
Similar: Adding custom message on Thank You page by shipping method in Woocommerce

Dispatch received orders woocommerce to dealers sending email notifications

I have a list of emails (dealers) and I need when I receive order in wp-admin i open this order and send this order to a dealer (commercial , user...). Every dealer have an email and mark this order in custom field that he have been send to this dealer.
In my woocommerce orders page I need to open a order and do something like this:
Order 001 --- > send to Email1#exemple.com = Order 001 - Sent to Email1#exemple.com
Order 002 ----> send to Email2#exemple.com = Order 002 - Sent to Email2#exemple.com
Order 003 --- > send to Email1#exemple.com = Order 003 - Sent to Email1#exemple.com
I don't know where to start.
Does anyone have an idea or some code to achieve something like this?
Thanks
Here is a complete answer that will feet your needs. You will have to set in the 2nd function the array of the emails and names from your dealer list.
This code will display in backend Order edit pages a custom metabox with a selector, were you will set a dealer and you will click on "Save Order"…
A New Order notification email will be sent just once to that dealer email address.
Here is the code:
//Adding Meta container admin shop_order pages
add_action( 'add_meta_boxes', 'my_custom_order_meta_box' );
if ( ! function_exists( 'my_custom_order_meta_box' ) )
{
function my_custom_order_meta_box()
{
global $woocommerce, $order, $post;
add_meta_box( 'dealer_dispatch', __('Dealer Dispatch','woocommerce'), 'add_order_custom_fields_for_packaging', 'shop_order', 'side', 'core' );
}
}
//adding Meta field in the meta container admin shop_order pages
if ( ! function_exists( 'add_order_custom_fields_for_packaging' ) )
{
function add_order_custom_fields_for_packaging()
{
global $woocommerce, $order, $post;
// Define HERE your array of values <== <== <== <== <== <== <== <==
$option_values = array(
'default' => __('no selection', 'woocommerce'),
'dealer1#email.com' => 'Dealer 1 Name',
'dealer2#email.com' => 'Dealer 2 Name',
'dealer3#email.com' => 'Dealer 3 Name',
);
// Get the values from the custom-fields (if they exist)
$meta_field_data = get_post_meta( $post->ID, '_dealer_dispatch', true );
$dealer_email_sent = get_post_meta( $post->ID, '_dealer_email_sent', true );
echo '<input type="hidden" name="my-custom-order_meta-box-nonce" value="'. wp_create_nonce() .'">
<label for="dealer_dispatch">'.__('Select a dealer', 'woocommerce').'</label><br>
<select name="dealer_dispatch">';
foreach( $option_values as $option_key => $option_value ){
if ( $meta_field_data == $option_key || 'default' == $option_key )
$selected = ' selected';
else
$selected = '';
echo '<option value="'.$option_key.'"'.$selected.'>'. $option_value.'</option>';
}
echo '</select><br>';
// if an email has been sent to the dealer we display a message
if( ! empty($dealer_email_sent) )
echo '<p style="color:green; font-weight:bold;">'.__('Email sent to: ', 'woocommerce').$dealer_email_sent.'</p>';
}
}
//Save the data of the Meta field
add_action( 'save_post', 'add_my_custom_field_for_order_meta_box', 20, 1 );
if ( ! function_exists( 'add_my_custom_field_for_order_meta_box' ) )
{
function add_my_custom_field_for_order_meta_box( $post_id ) {
## Verify and securing data. ##
// Check if our nonce is set.
if ( ! isset( $_POST[ 'my-custom-order_meta-box-nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'my-custom-order_meta-box-nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
// Continuing only if form is submited.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check and set the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// -- -- IT IS SECURED NOW -- --
// Sanitize input and update order meta data custom field.
$dealer_dispatch = $_POST[ 'dealer_dispatch' ];
// Saving the selected value
if( 'default' != $dealer_dispatch )
update_post_meta( $post_id, '_dealer_dispatch', sanitize_text_field( $dealer_dispatch ) );
# SEND CUSTOM EMAIL ONLY ONCE #
$dealer_dispatch_val = get_post_meta( $post_id, '_dealer_dispatch', true );
$dealer_email_sent = get_post_meta( $post_id, '_dealer_email_sent', true );
if( empty($dealer_email_sent) && !empty($dealer_dispatch_val) ){
$email_notifications = WC()->mailer()->get_emails();
$email_notifications['WC_Email_New_Order']->recipient = $dealer_dispatch;
$email_notifications['WC_Email_New_Order']->trigger( $post_id );
// Creating a custom meta data for this order to avoid sending this email 2 times
update_post_meta( $post_id, '_dealer_email_sent', $dealer_dispatch_val );
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

Categories