1

I am trying to display the data for the last customer order on a custom page using a shortcode. With the help of resources from this platform, I have managed so far to display order ID, subtotal, and total. Displaying the order title (or the title of the product/item in the order) has taken me hours. I have tried ->get_name as you can see in the commented section, but failed. I tried so many, including post_title but failed. Your help will be appretiated.

    if ( is_user_logged_in() ){
        $customer_user_id = get_current_user_id(); 
        $last_order = wc_get_customer_last_order( $customer_user_id);
        if ( !empty( $last_order ) ) {
            $order_id = $last_order->get_id();
            $withdrawqr_subtotal =$last_order->get_subtotal();
            $withdrawqr_total = $last_order->get_total();
            $withdrawqr_fee = $withdrawqr_total-$withdrawqr_subtotal;
            //$withdrawqr_agent =$last_order->get_name();
            $withdrawqr_id = $last_order->get_id();
            $withdrawqr_code =str_shuffle("DANTE");
            
    
            $withdrawqr_date = $last_order->get_date_created()->format('Y-m-d H:i:s');
            $retun_this_withdraw_text = "
        <html>
            <head>
                <title>Summary</title>
            </head>
            <body>
            <p>Confirmed <strong>".$withdrawqr_code."".$withdrawqr_id."</strong>. You have withdrawn<strong> KES ".$withdrawqr_subtotal." </strong>from <strong>".$withdrawqr_agent." </strong>on <strong>".$withdrawqr_date. "</strong>. You have been charged <strong>KES ".$withdrawqr_fee."</strong> withdrawal fee. Thank you</p>

            
            </body>
        </html>
    ";
            
            return $retun_this_withdraw_text;
        }
    }
}

add_shortcode('withdrawqr_get_last_order_details','withdrawqr_get_last_order_details');     

  • A shortcode is supposed to return a snippet of HTML code, that gets inserted somewhere into a full HTML document. So having your shortcode return `html`, `head` and `body` elements, that makes no sense to begin with - you will only create broken HTML as a result. – CBroe Aug 03 '22 at 07:05
  • 1
    _"Displaying the order title (or the title of the product/item in the order)"_ - and order itself does not have a "title". And an order can normally have more than one item (unless you specifically restricted that somehow?) - so you should get the list of items first of all, using the `get_items` method. And then you loop over those items, or pick the first/last one, and get the name(s) from those; details up to you. – CBroe Aug 03 '22 at 07:08
  • @CBroe, I know get_items method should work, but i just dont know how i can change my entire code to do that. Remember am getting the order details of the last order, not any other order. So am somehow confused how to use get_items method for last order. If you could rewrite my code to fit that, I will be much greatful. As for your first comment about html, head, and body elements, am not advanced in php, but somehow what i have is working for me already, except for getting the order product title. – George Obonyo Aug 03 '22 at 09:38
  • _"So am somehow confused how to use get_items method for last order."_ - same as you are already doing in that code, when it comes to getting the last order's id, subtotal, total ...? – CBroe Aug 03 '22 at 10:04
  • @CBroe, so $last_order = wc_get_customer_last_item( $customer_user_id);, would work? – George Obonyo Aug 03 '22 at 10:16
  • If `wc_get_customer_last_item` was actually an exiting function with the purpose of what its name says ... But I can not find _any_ results for that name on the web. – CBroe Aug 03 '22 at 10:17
  • @CBroe, I got the answer, but i can nolonger add it here because someone who didn't understand this question well already associated it with another question, which is answered. thanks for your contribution friend. – George Obonyo Aug 03 '22 at 11:35

0 Answers0