0

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

1
  • 2
    The examples you provide don't use get_posts() to get the $order variable they're using; where they show $order's origins, it seems to be that they're using the WooCommerce function wc_get_order().. get_posts() returns an array of WP_Post objects, and there's no get_items() method (ie, function) defined in WP_Post. For more info on using wc_* functions, you'll need to check with WooCommerce support.
    – Pat J
    Commented Apr 16, 2022 at 22:11

1 Answer 1

0

I found a solution to the problem, here is the code below. I just can't get the pagination to work yet. Everything else works fine.

Shortcode

add_shortcode( 'all_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('Completed', 'Order status', 'woocommerce'))
)));  
    
$total_records = count($customer);
$posts_per_page = 3;
$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('Completed', 'Order status', 'woocommerce'))
));

     // Get Access $order variable Foreach
     foreach ( $customer_orders as $order_id ) {
        
     // Get $product object from $order / $order_id
     $order = wc_get_order( $order_id );
     $items = $order->get_items();
     
     $orders_id = $order->get_id();
     $status = $order->get_status();
     $date_created = $order->get_date_created()->date('d/m/Y - H:i');
     $payment_method = $order->get_payment_method_title();
     $order_total = $order->get_formatted_order_total();
    
    // Get Access Items Variable Foreach
     foreach ( $items as $item ) {
     $product_name = $item->get_name();
         
     // Get product image - https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
     $product = $item->get_product();
     if( $product instanceof WC_Product ){
     $order_img = $product->get_image();
     }
    
    // Get product download button 
    $downloads = $order->get_downloadable_items();
     if(is_array($downloads)){
     foreach($downloads as $product){
     $download_button = '<a href="'. $product['download_url'] .'" target="_blank">Download</a>';
     } 
    }   
               
     echo 
     '
     <div class="container_orders">
    
      <div class="container_orders_card_left"> 
        <div class="items_orders_card name"> '. $product_name .' </div>
        <div class="items_orders_card"> <span class="items_title">Ordine:</span> #'. $orders_id .'  </div>
        <div class="items_orders_card"> <span class="items_title">Effettuato il:</span> '. $date_created .' </div>
        <div class="items_orders_card"> <span class="items_title">'. $payment_method .'</span> • '. $order_total .' </div>
        <div class="items_orders_card"> <span class="items_title">Stato:</span> '. $status .' </div>
      </div>
    
      <div class="container_orders_card_right">
        <div class="container_orders_img"> '. $order_img .' </div>
        <div class="container_orders_download"> '. $download_button .' </div>
      </div>
    
     </div>
     
     '; 
     
    }} // Closing Foreach

    ?><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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.