Home > database >  Woocommerce - Trying to get property 'max_num_pages' of non-object in orders.php
Woocommerce - Trying to get property 'max_num_pages' of non-object in orders.php

Time:04-19

I have already asked a question that contains part of this code, but here I raise a totally different problem.

I'm trying to customize the orders.php template, I get id, names, images and everything I need to show all orders, work fine. The problem is this message - Notice: Trying to get property 'max_num_pages' of non-object in /...path_file.../orders.php on line 62.

Now, on line 62 of my custom orders.php I have the following code ?><?php if ( 1 < $customer->max_num_pages ) : ?>

What I'm trying to do is get pagination and limit number orders displayed, so I put the code below in functions.php. It works with the original orders.php template, but with my customization it doesn't work due to the message I'm getting.

Can anyone help me figure out what I'm doing wrong? I have been checking the problem for several days but I cannot solve it. Maybe I should enter an additional foreach, but I'm not sure.

In functions.php

add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_account_orders', 10, 1 );
function custom_my_account_orders( $args ) {
    // Set the post per page
    $args['limit'] = 2;

    return $args;
}

In orders.php (customized)

// Get all orders for the current user.
$customer = wc_get_orders(['customer_id' => get_current_user_id(),]);

     // Get Access $order variable Foreach
     foreach ( $customer 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 & Product 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 >
    
      <div > 
        <div > '. $product_name .' </div>
        <div > <span >Ordine:</span> #'. $orders_id .'  </div>
        <div > <span >Effettuato il:</span> '. $date_created .' </div>
        <div > <span >'. $payment_method .'</span> • '. $order_total .' </div>
        <div > <span >Stato:</span> '. $status .' </div>
      </div>
    
      <div >
        <div > '. $order_img .' </div>
        <div > '. $download_button .' </div>
      </div>
    
     </div>
     
     '; 
     
     }} // Closing Foreach

//The piece of code below I took from the original orders.php template. This works with the filter I put in functions.php. It is this code below that shows the prev/next buttons for pagination.
     
     ?><?php if ( 1 < $customer->max_num_pages ) : ?>
        <div >
            <?php if ( 1 !== $current_page ) : ?>
                <a  href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page - 1 ) ); ?>"><?php esc_html_e( 'Previous', 'woocommerce' ); ?></a>
            <?php endif; ?>

            <?php if ( intval( $customer->max_num_pages ) !== $current_page ) : ?>
                <a  href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page   1 ) ); ?>"><?php esc_html_e( 'Next', 'woocommerce' ); ?></a>
            <?php endif; ?>
        </div>
     <?php endif; ?>

CodePudding user response:

I have edited abit your code. Tested it and its working - https://prnt.sc/OYYSO5a1eM7O , https://prnt.sc/NomOevXbMqL9

<?php
defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_before_account_orders', $has_orders ); ?>

<?php if ( $has_orders ) : ?>
<?php

    foreach($customer_orders->orders as $customer_order) {
        $order = wc_get_order( $customer_order );
        $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 & Product Variable Foreach
     foreach ( $items as $item ) {
        $product_name = $item->get_name();
        $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 >';
            echo '<div >'; 
                echo '<div > '. $product_name .' </div>'; 
                echo '<div > <span >Ordine:</span> #'. $orders_id .'  </div>'; 
                echo '<div > <span >Effettuato il:</span> '. $date_created .' </div>'; 
                echo '<div > <span >'. $payment_method .'</span> • '. $order_total .' </div>'; 
                echo '<div > <span >Stato:</span> '. $status .' </div>'; 
            echo '</div>';
            echo '<div >'; 
                echo '<div > '. $order_img .' </div>'; 
                if($downloads) {
                    echo '<div > '. $download_button .' </div>'; 
                }
            echo '</div>'; 
         echo '</div>'; 

    }}?>
    <?php if ( 1 < $customer_orders->max_num_pages ) : ?>
        <div >
            <?php if ( 1 !== $current_page ) : ?>
                <a  href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page - 1 ) ); ?>"><?php esc_html_e( 'Previous', 'woocommerce' ); ?></a>
            <?php endif; ?>

            <?php if ( intval( $customer_orders->max_num_pages ) !== $current_page ) : ?>
                <a  href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page   1 ) ); ?>"><?php esc_html_e( 'Next', 'woocommerce' ); ?></a>
            <?php endif; ?>
        </div>
    <?php endif; ?>
<?php endif; ?>
<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>

Example 2 with number pagination

<?php
defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_before_account_orders', $has_orders ); ?>

<?php if ( $has_orders ) : ?>
<?php

    foreach($customer_orders->orders as $customer_order) {
        $order = wc_get_order( $customer_order );
        $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 & Product Variable Foreach
     foreach ( $items as $item ) {
        $product_name = $item->get_name();
        $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 >';
            echo '<div >'; 
                echo '<div > '. $product_name .' </div>'; 
                echo '<div > <span >Ordine:</span> #'. $orders_id .'  </div>'; 
                echo '<div > <span >Effettuato il:</span> '. $date_created .' </div>'; 
                echo '<div > <span >'. $payment_method .'</span> • '. $order_total .' </div>'; 
                echo '<div > <span >Stato:</span> '. $status .' </div>'; 
            echo '</div>';
            echo '<div >'; 
                echo '<div > '. $order_img .' </div>'; 
                if($downloads) {
                    echo '<div > '. $download_button .' </div>'; 
                }
            echo '</div>'; 
         echo '</div>'; 

    }}?>
    <?php 
    $args = array(
        'base'          => esc_url( wc_get_endpoint_url( 'orders') ) . '%_%',
        'format'        => '%#%',
        'total'         => $customer_orders->max_num_pages,
        'current'       => $current_page,
        'show_all'      => false,
        'end_size'      => 3,
        'mid_size'      => 3,
        'prev_next'     => true,
        'prev_text'     => '&larr;',
        'next_text'     => '&rarr;',
        'type'          => 'list',
        'add_args'      => false,
        'add_fragment'  => ''
    );
    echo paginate_links($args); ?>
<?php endif; ?>
<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>
  • Related