0

I'm trying to add the meta Ready_by to an order by taking the time set by Order Due Date and deducting an hour from it. This Order Due Date is set using the plugin Order Delivery Date Pro, which adds a calendar date/time picker to checkout.

I've been trying between the hooks woocommerce_checkout_create_order and woocommerce_thankyou and couldn't get it work.

Here is the code I have so far in functions.php:

// Assign default ready by time by deducting an hour from the delivery due time
add_action('woocommerce_thankyou', 'define_default_ready_by_time', 10, 1);

function define_default_ready_by_time( $order_id ) {
  $order = wc_get_order($order_id);

  $order_due_time = get_post_meta($order, 'Order Due Date', true);
  $ready_by_default = date('H:i',strtotime('-1 hour',strtotime(date('Y-m-d H:i:s',strtotime(str_replace('/','-',$order_due_time))))));

  $order->update_meta_data( 'Ready_by', $ready_by_default);
}

If anyone can shed a light on this, it'd be greatly appreciated. Thank you!

Wallace L.
  • 41
  • 1
  • 10

1 Answers1

0

On this line

$order_due_time = get_post_meta($order, 'Order Due Date', true);

$order is an object where the post ID is required. See here So you could use

$order_due_time = get_post_meta($order_id, 'Order Due Date', true);

Alternatively

This answer gives a more in depth description, but the following code should work.

add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {

    $order_due_time = $order->get_meta('_order_due_date'); //better way to get order meta

    //I'm unsure if this works from your code, need to test it, but assuming it does
    $ready_by_default = date('H:i',strtotime('-1 hour',strtotime(date('Y-m-d H:i:s',strtotime(str_replace('/','-',$order_due_time))))));


    $order->update_meta_data( '_order_due_date', $ready_by_default  );
}
Lyle Bennett
  • 90
  • 1
  • 9