display product description in my account orders table of Woocommerce

三世轮回 提交于 2019-12-11 15:26:45

问题


I want to display the description of the first item only next to the order id so if there is 3 items and all of them have descriptions i want it to show next to the order id the description of the first item only.

Normally: #3214

What i expect: #3214 | Product Description


回答1:


Add the follows code snippet in your active theme's functions.php to dothe above -

function modify_woocommerce_order_number( $order_id, $order ) {
    if( !is_wc_endpoint_url( 'orders' ) ) return $order_id;
    $first_line_item_descrp = '';
    foreach ( $order->get_items( 'line_item' ) as $item_id => $item ) {
        $product      = $item->get_product();
        $first_line_item_descrp = $product->get_description();
        break;
    }
    return $order_id . ' | ' . $first_line_item_descrp;
}
add_filter( 'woocommerce_order_number', 'modify_woocommerce_order_number', 99, 2 );



回答2:


This if its on a separate column but can you help to modify this code to make it not next to order id but in a separate coulmn after the order id ?, i have a code below but it gets the name and just need a little modification to get the description of the first product only not all the products

to give the same equal value provided in the snippet before this answer but in a separate coulmn

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-number' === $key ) {
            $new_columns['order-items'] = __( 'Event', '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 ) {


$first_line_item_descrp = '';
foreach ( $order->get_items( 'line_item' ) as $item_id => $item ) {
$product = $item->get_product(); 

$first_line_item_descrp = $product->get_description();
break;
}
echo $first_line_item_descrp;
} 


来源:https://stackoverflow.com/questions/58395029/display-product-description-in-my-account-orders-table-of-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!