Search code examples
phpwordpresswoocommerceshortcode

How to put the woocommerce order date in a shortcode


I'm trying to put the date of the order placed on woocommerce in a shortcode.

This is what I did:

add_shortcode( 'data-pagamento' , 'data_pgmnt' ); 
function data_pgmnt() { 
  $customer_id = get_current_user_id(); 
  $order = jet_woo_builder_template_functions()->get_current_received_order(); 
  $date_paid =  $order->get_date_paid();
  return $date_paid->date("j/m/Y");
}

In the past this works, now for some reason it doesn't. Am I doing something wrong ?


Solution

  • I found a solution after several attempts. I found that I was formatting the code wrong, consequently I wrote like this:

    add_shortcode( 'data-pagamento' , 'data_pgmnt' );
    function data_pgmnt() {
        $customer_id = get_current_user_id();
        $order = jet_woo_builder_template_functions()->get_current_received_order();
        $date = $order->get_date_created()->format ('j/m/Y – g:i A');
        return $date;
    }
    

    I can now see the order date on the thank you page. I placed the code in the child theme's functions.php file.

    get_date_paid breaks the page layout. I suppose this happens because I paid with paypal sandbox, so there is no actual payment date.

    Reference: https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/