Home Forums Backend Issues (wp-admin) Calculate PHP date_diff and save to ACF Field during post creation


Calculate PHP date_diff and save to ACF Field during post creation

  • I am facing the following challenge right now, if someone could help met out I would really appreciate it.

    I have a WooCommerce store which automatically creates a new post after every order which is completed. These orders are saved into a Custom Post Type, with the order details in an ACF repeater field. For this function I used the undermentioned code.

    Right now I want to add an extra function to my code which calculates the days between the order date and today. The order date is equal to the published date of the stored post. So in fact the calculation should be based on the published date and today. After the calculation is made, I want to store the days into an ACF numbers field.

    For example if someone ordered a product on the first of november and today it is the 24th of november, the outcome should be 23.

    The code I use right now.

    //Create post
    function create_post_after_order( $order_id ) {
      if (  $order_id instanceof WC_Order ){
    //Find items based on orderID
      $order = wc_get_order( $order_id );
      $order_items = $order->get_items(); 
    //Loop through items
        foreach ( $order_items as $item_id => $item_data ) {
        $product_ids[]       = $item_data->get_product_id();
        $product_names[]     = $item_data->get_name();
        $product_quantities[] = $item_data->get_quantity(); 
        $ordeline_subtotals[]     = $item_data->get_subtotal(); 
        $product_details = $item_data->get_product();
        // Get the product price that customer paid
        $product_prices[]        = $product_details->get_price(); 
        //Get sale price (i.e discounted price, if it exists)
        $product_regular_price  = $product_details->get_sale_price(); 
        //Regular price.
        $product_sale_price     = $product_details->get_regular_price();
    //Create actual post
        $new_post = array(
        'post_title' => "Order {$order_id}", 
        'post_date' => date('Y-m-d H:i:s'), 
        'post_author' => $user_ID, 
        'post_type' => 'groeiproces', 
        'post_status' => 'publish',
      $post_id = wp_insert_post($new_post);
    //Connect ACF Fields
        $orderdetails_key = 'field_61645b866cbd6';
        $product_id_key = 'field_6166a67234fa3';
        $product_name_key = 'field_61645b916cbd7';
        $product_price_key = 'field_6166a68134fa4';
        $product_quantity_key = 'field_6165bd2101987';
        $ordeline_subtotal_key = 'field_6166a68934fa5';
        $product_id = $product_ids;
        $product_name = $product_names;
        $product_price = $product_prices;
        $product_quantity = $product_quantities;
        $ordeline_subtotal = $ordeline_subtotals;
    //Save orderdata in ACF repeater field
    foreach ($product_id as $index => $product_id) {
        $orderdetails_value[] = array(
        $product_id_key => $product_id, 
        $product_name_key => $product_name[$index],
        $product_price_key => $product_price[$index],
        $product_quantity_key => $product_quantity[$index],
        $ordeline_subtotal_key => $ordeline_subtotal[$index],
        update_field( $orderdetails_key, $orderdetails_value, $post_id );
    add_action( 'woocommerce_thankyou', 'create_post_after_order', 10, 1 );
  • You would need to use PHP’s date_diff

    Grabbing the date from the order and todays date.

    Once you have the value, you can use update_post_meta() to pass the value to your post

  • Could you give me an insight how I can implement this in my code. I am not that advanced in PHP yet.

  • Hmm, something like:

    $now = time(); 
    $date_created = $order->get_date_created();
    $dt = new DateTime($date_created);
    $date = $dt->format('Y-m-d');
    $your_date = strtotime($date);
    $datediff = $now - $your_date;
    $diff = round($datediff / (60 * 60 * 24));
    update_post_meta( $post_id, 'custom_field', $diff );

    Amend custom_field to your field name, code untested

  • So how can I combine that with my current code. Because this is a different field it shouldn’t be part of the repeater field. Could you help me on that?

  • In theory, you should be able to add it below:
    $post_id = wp_insert_post($new_post);

    Reason being, you’ve accessed the order info, you’ve created the post and have the ID.

    The only part you need to check is that date formats are correct and you change the custom_field name to your field.

    Code is untested, so you may need to check, test and debug by echoing out various points to see if it works

  • Thanks men! Was able to get it working. Really appreciate your help!

  • Unfortunately the value seems to not update everyday. Does anyone if I should upgrade the code or add some cron job or something?

    $new_post = array(
        'post_title' => "Order {$order_id}", // Definieer titel van de post
        'post_date' => date('Y-m-d H:i:s'), // Voeg publicatiedatum toe
        'post_author' => $user_ID, // Definieer de klant als auteur
        'post_type' => 'groeiproces', // Definieer CPT
        'post_status' => 'publish', // Publiceer post
      $post_id = wp_insert_post($new_post);
    	$now = time(); 
    	$date_created = $order->get_date_created();
    	$dt = new DateTime($date_created);
    	$date = $dt->format('Y-m-d');
    	$your_date = strtotime($date);
    	$datediff = $now - $your_date;
    	$diff = round($datediff / (60 * 60 * 24));
    	update_post_meta( $post_id, 'datum', $diff );
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.