Site icon Hip-Hop Website Design and Development

Getting the date_diff perform return the distinction in days

I have been attempting to construct a characteristic to rely the times it takes for merchandise to be offered. We’re promoting distinctive gadgets on behalf of our prospects so it is good to know for reporting and product administration functions which gadgets truly promote and the way lengthy it takes.

The customized perform appears to getting the info for $order_created_date and $product_date OK and storing it within the order merchandise meta, however truly calculating the distinction in days is just not working.

That is what I’ve achieved thus far:

perform mysite_woocommerce_order_completed( $order_id ) {
    //get order created date and order gadgets
    $order = NULL;
    $order_created_date = NULL;
    $order = wc_get_order($order_id);
    $order_created_date = $order->order_date;
    $order_items = $order->get_items();
    foreach ($order_items as $item_id => $order_item) {
        // addin _order_date meta not wanted in completed model
        $product_id = $order_item['product_id'];
        wc_add_order_item_meta( $item_id, '_order_added_date', $order_created_date );
        // including _product_published_date meta not wanted in completed model
        $product_date = get_the_time( 'Y-m-d H:i:s', $product_id ); 
        wc_add_order_item_meta( $item_id, '_product_published', $product_date );
        // calculate date distinction, not working. Wants a test to not add the meta if already added as a result of makes duplicate entries
        $diff = date_diff( $product_date, $order_created_date );
        wc_add_order_item_meta( $item_id, '_days_to_purchase', $diff );
    }
}

add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_order_completed', 1, 1 );

Any assist to unravel the issue and suggestions to optimize the code could be appreciated. Cheers!