php – How to get woocommerce orders with get_posts method in a shortcode

Question

I’m writing shortcode which must show all orders placed by current user. I followed this two guidee to get them:

  1. https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/

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

However shortcode not working. Get the following error on line 31 of my file – Fatal error: Uncaught Error: Call to undefined method WP_Post::get_items().

On line 31 i have this: foreach ( $order->get_items() as $item_id => $item )

I’m relatively new to this, I don’t understand what I’m doing wrong, can anyone help me correct this mistake? I appreciate any help.

My Shortcode

// Start Shortcode.
add_shortcode( 'orders' , 'prc_orders' );
function prc_orders() {
    
// Get all orders for the current user.
$customer = get_posts(apply_filters('woocommerce_my_account_my_orders_query', array(
    'numberposts' => -1,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => wc_get_order_types('view-orders'),
    'post_status' => array_keys(wc_get_order_statuses())
)));  
    
$total_records = count($customer);
$posts_per_page = 5;
$total_pages = ceil($total_records / $posts_per_page);
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$customer_orders = get_posts(array(
    'meta_key' => '_customer_user',
    'meta_value' => get_current_user_id(),
    'post_type' => wc_get_order_types('view-orders'),
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'post_status' => array_keys(wc_get_order_statuses())
));

    foreach ( $customer as $order ) {
    foreach ( $order->get_items() as $item_id => $item ) {
    
    // Get Order info elements - https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
    $order_id = $order->get_id();
    $status = $order->get_status();
    $date_created = $order->get_date_created()->date('d/m/Y - H:i');
     
    // Get and Loop Over Order Items Variable - https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
    $product_name = $item->get_name();
    
    echo '<div class="items_orders_card name"> '. $order_id .' '. $product_name .' '. $status .' '. $date_created .' </div>';  
    }} 


    ?><div class="pagination"><?php
    $args = array(
        'base' => '%_%',
        'format' => '?pagina=%#%',
        'total' => $total_pages,
        'current' => $paged,
        'show_all' => False,
        'end_size' => 5,
        'mid_size' => 5,
        'prev_next' => True,
        'prev_text' => __('&laquo; Previous'),
        'next_text' => __('Next &raquo;'),
        'type' => 'plain',
        'add_args' => False,
        'add_fragment' => ''
    );
    echo paginate_links($args);
    ?></div><?php

} // End Shortcode

0
Snake 4 months 2022-04-16T13:13:19-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse