Blog

  • Adding Products Column to WooCommerce My Orders Page

    Posted at 21 Jul, 2018 in Tutorials

    It will be very helpful to customers if we show the products purchased by them in their My Account > Orders page itself and you can achieve this using couple of functions below.

    Step 1:

    Paste the following code in your theme’s ‘functions.php’. This will add a new column named ‘Products’ next to ‘Total’.

    /** function to add new column **/
    function add_products_my_account_orders_column( $columns ) {
    $new_columns = array();
    foreach ( $columns as $key => $name ) {
    $new_columns[ $key ] = $name;
    // add products after order total column
    if ( 'order-total' === $key ) {
    $new_columns['order-products'] = __( 'Products', 'textdomain' );
    }
    }
    return $new_columns;
    }
    add_filter( 'woocommerce_my_account_my_orders_columns', 'add_products_my_account_orders_column' );

    Feel free to change the column order (just you need to change the corresponding $key above.

    Step 2:

    Now we need to show the products data in our new column which was too very simple. Again, add the following to your theme’s ‘functions.php’.

    /** function to display products **/
    function add_products_data_my_account_orders_column( $order ) {
    //loop through products of the order
    foreach( $order->get_items() as $item_id => $item ) {
    $product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
    $is_visible = $product && $product->is_visible();
    $product_permalink = apply_filters( 'woocommerce_order_item_permalink', $is_visible ? $product->get_permalink( $item ) : '', $item, $order );
    echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '<p><a href="%s">%s</a>', $product_permalink, $item['name'] ) : $item['name'], $item, $is_visible );
    echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '× %s', $item['qty'] ) . '</strong></p>', $item );
    }
    }
    add_action( 'woocommerce_my_account_my_orders_column_order-products', 'add_products_data_my_account_orders_column' );

    That’s it and the products data will start appearing to our new ‘Products’ column.

    If you want to show all the meta information code too, add this to the add_products_data_my_account_orders_column() function.

    do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
    $order->display_item_meta( $item );
    $order->display_item_downloads( $item );
    do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );

Categories

Categories

Archives

Archives