4

I have a Woocommerce shop and I wanted to add a delivery_date after I accept the payment.

I create a custom field in the order section named delivery_date with a date value.

Now I wanted to use this custom field as a placeholder in email notification subject, like:

Your order is now {order_status}. Order details are shown below for your reference: deliverydate: {delivery_date}

I think the placeholder don't work like this, I need to change something in the php but I don't know where.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Kim
  • 55
  • 1
  • 4

2 Answers2

4

To add a custom active placeholder {delivery_date} in woocommerce email subject, you will use the following hooked function.

You will check before, that delivery_date is the correct post meta key used to save the checkout field value to the order (check in wp_postmeta database table for the order post_id).

The code:

add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 10, 2 );
function add_custom_email_format_string( $string, $email ) {
    $meta_key    = 'delivery_date'; // The post meta key used to save the value in the order
    $placeholder = '{delivery_date}'; // The corresponding placeholder to be used
    $order = $email->object; // Get the instance of the WC_Order Object
    $value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : ''; // Get the value

    // Return the clean replacement value string for "{delivery_date}" placeholder
    return str_replace( $placeholder, $value, $string );
}

Code goes in function.php file of your active child theme (or active theme). It should works.

Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {delivery_date}

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • get a error Warning: array_values() expects parameter 1 to be array, null given its on the return line i putted the code into a php snippe, i think it should work there too or? – Kim Apr 08 '19 at 13:04
  • @Kim I have no errors… I have made a light update… You need to be sure that the correct meta_key used on wp_postmeta database table is `delivery_date`… If not uou need to change it in the function code. – LoicTheAztec Apr 08 '19 at 13:21
0

If you want to print value of "delivery_date" in email content then you can do like this.

$content = "Your order is now %%order_status%%. Order details are shown below for your reference: deliverydate: %%delivery_date%%";
$search_array = ["{order_status}","{delivery_date}"]
$replace_array = [$valueOfOrderStatus,$valueOfDeliveryDate];
$content = str_replace($search_array, $replace_array, $content);
Adnan Rasheed
  • 866
  • 8
  • 12
  • Using this code you can all replace all variables in the content by just adding all search and replace items to the search and replace arrays. – Adnan Rasheed Apr 08 '19 at 13:46