Skip to content
Advertisement

Add permalink to product title in WooCommerce custom order history

I am using Show products name and quantity in a new column on Woocommerce My account Orders answer code to add an additional column to the Woocommerce My Account Order History page that displays the titles of items for the order.

How would I make the code display the title as a hyperlink to the product page of the item ordered rather than just plain text?

Advertisement

Answer

To make each product title linked to the product, you can use the following:

add_filter( 'woocommerce_my_account_my_orders_columns', 'additional_my_account_orders_column', 10, 1 );
function additional_my_account_orders_column( $columns ) {
    $new_columns = [];

    foreach ( $columns as $key => $name ) {
        $new_columns[ $key ] = $name;

        if ( 'order-status' === $key ) {
            $new_columns['order-items'] = __( 'Items', 'woocommerce' );
        }
    }
    return $new_columns;
}

add_action( 'woocommerce_my_account_my_orders_column_order-items', 'additional_my_account_orders_column_content', 10, 1 );
function additional_my_account_orders_column_content( $order ) {
    $details = array();

    foreach( $order->get_items() as $item )
        $details[] = '<a href="' . $item->get_product()->get_permalink() . '">' . $item->get_name() . '</a>&nbsp;&times;&nbsp;' . $item->get_quantity();

    echo count( $details ) > 0 ? implode( '<br>', $details ) : '&ndash;';
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement