skip to Main Content

I want to display the order ID, order date and product image in the My Account download page.

add_filter( 'woocommerce_account_downloads_column_download-product', 'display_product_image_on_account_downloads' );
function display_product_image_on_account_downloads( $download ) {
    // Targeting view order pages only
    if ( ! is_wc_endpoint_url( 'downloads' ) ) return;

    if ( $download['product_id'] > 0 ) {
        $product = wc_get_product( $download['product_id'] ); 
        $image   = $product->get_image( array(324, 194) ); // The product image
        $order_id = $order->get_id(); // The order id

        if ( $download['product_url'] ) {
            echo $image . '<a href="' . esc_url( $download['product_url'] ) . '">' . esc_html( $download['product_name'] ) . '</a>';
        echo '<p>' . esc_html( $order_id ) . '</p>';
            echo '<p>' . esc_html( wc_format_datetime( $order->get_date_created() ) ) . '</p>'; 
        } else {
            echo $image . esc_html( $download['product_name'] );
        }
    }
}

The product image is displayed, but the order ID and order date do not display properly.
Is there any way of achieving this? Thanks.

2

Answers


  1. I carefully check your code and realized it has some problems. So you can use these version:

    add_filter( 'woocommerce_account_downloads_column_download-product', 'display_product_image_order_info_on_account_downloads', 10, 2 );
    function display_product_image_order_info_on_account_downloads( $download, $item ) {
        // Targeting view order pages only
        if ( ! is_wc_endpoint_url( 'downloads' ) ) return;
    
        if ( $download['product_id'] > 0 ) {
            $product    = wc_get_product( $download['product_id'] );
            $image      = $product->get_image( array( 324, 194 ) ); // The product image
            $order_id   = $item->get_order_id(); // Get the order ID from the $item object
            $order_date = $item->get_order()->get_date_created(); // Get the order date from the $item object
    
            if ( $download['product_url'] ) {
                echo $image . '<a href="' . esc_url( $download['product_url'] ) . '">' . esc_html( $download['product_name'] ) . '</a>';
                echo '<p>' . esc_html( $order_id ) . '</p>';
                echo '<p>' . esc_html( wc_format_datetime( $order_date ) ) . '</p>'; 
            } else {
                echo $image . esc_html( $download['product_name'] );
            }
        }
    }
    

    You can use it on your function.php on your child-theme.

    Login or Signup to reply.
  2. First, woocommerce_account_downloads_column_download-product is an action hook and the variable $order is not defined in your code.

    Use instead this revisited code version:

    add_action( 'woocommerce_account_downloads_column_download-product', 'display_product_image_on_account_downloads' );
    function display_product_image_on_account_downloads( $download ) {
        // Targeting view order pages only
        if ( ! is_wc_endpoint_url( 'downloads' ) ) return;
    
        if ( $download['product_id'] > 0 ) {
            $product = wc_get_product( $download['product_id'] ); 
            $image   = $product->get_image( array(324, 194) ); // The product image
    
            if ( $download['product_url'] ) {
                echo $image . '<a href="' . esc_url( $download['product_url'] ) . '">' . esc_html( $download['product_name'] ) . '</a>';
    
                // Here you get the order ID
                if ( $download['order_id'] > 0 ) {
                    // Get an instance of the WC_Order object
                    $order = wc_get_order( $download['order_id'] );
    
                    echo '<p>' . esc_attr( $download['order_id'] ) . '</p>'; 
                    echo '<p>' . esc_html( wc_format_datetime( $order->get_date_created() ) ) . '</p>'; 
            } else {
                echo $image . esc_html( $download['product_name'] );
            }
        }
    }
    

    It should work now.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search