Lyall
Lyall

Reputation: 1437

Get WooCommerce order date time on emails

I have the below function which I've added to a couple of places on the front end of my site, to show estimated delivery date based on the current date:

<?php $del_from = date('jS F', strtotime("+10 days"));
      $del_to = date('jS F', strtotime("+30 days"));
      $html = "Order today for estimated delivery between <b>{$del_from}</b> and <b>{$del_to}</b>. <a href='deliveryinfopageURL' target='_blank'>Read more about delivery</a>.";

   echo $html;
?>

I would like to include this in the shipment notification email (with slightly different text), but rather than using the current date + X days I'd like to use the order date + X days instead, as that's when the fulfilment process starts. Is this possible?

Also is there anything else that would need to change in order for this to work in a WooCommerce email template? Such as changing the way the hyperlink is placed in the text?

Upvotes: 2

Views: 3711

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253978

In most Email templates or email hooks, the WC_Order object $order is accessible, so you can use one of the following WC_Order methods:

  • get_date_created(),
  • get_date_modified(),
  • get_date_paid(),
  • get_date_completed()

and you will get WC_DateTime object that you can use in your code like:

<?php 
    $order_datetime  = $order->get_date_created(); // Get order created date ( WC_DateTime Object ) 
    $order_timestamp = $order_datetime->getTimestamp(); // get the timestamp in seconds
    $day             = 86400; // 1 day in seconds

    $delivery_url    = 'deliveryinfopageURL'; // <== Set the correct URL to the delivery page
    $delivery_txt    = __("Read more about delivery", "woocommerce");

    // Output / display
    printf( 
        __('Order today for estimated delivery between %s and %s. %s', "woocommerce"),
        '<strong>'.date('jS F', $order_timestamp + (10 * $day) ).'</strong>',
        '<strong>'.date('jS F', $order_timestamp + (30 * $day) ).'</strong>',
        '<a href="'.$delivery_url.'" target="_blank">'.$delivery_txt.'</a>'
    );
?>

It should work on email templates.

If the WC_order object is not accessible, but you can access the related WC_email object $email and get the WC_Order object with: $order = $email->object;

Upvotes: 2

Related Questions