2
votes

I'm trying to add my product short description into my order page as a new tab so we have an easier way to order products without having to go inside of them.

I see currently SKU displays under the product ideally it would have a Product short description instead.

This is what I've managed to get so far however no output of the short desc

// Adds tab 
add_action( 'woocommerce_admin_order_item_headers', 'pd_admin_order_items_headers' );
function pd_admin_order_items_headers($order){
  ?>
  <th class="line_customtitle sortable" data-sort="your-sort-option">
    Product MPN
  </th>
  <?php
}

// Shows Short Desc
add_action( 'woocommerce_admin_order_item_values', 'pd_admin_order_item_values' );
function pd_admin_order_item_values( $product ) {
  ?>
  <td class="line_customtitle">
    <?php the_excerpt(); ?>
  </td>
  <?php
}

The results currently say

"There is no excerpt because this is a protected post."

I feel like it's not looping through the product and trying to fetch the orders excerpt hence why it says it's protected but I'm not too experienced with this.

Any help is appreciated.

2

2 Answers

1
votes

Try the following instead (Code is commented):

// Add a custom column to the order "line items" html table
add_action( 'woocommerce_admin_order_item_headers', 'custom_admin_order_items_headers', 20, 1 );
function custom_admin_order_items_headers( $order ){

    echo '<th class="line_custom-title sortable" data-sort="your-sort-option">';
    echo __('Short description', 'woocommerce') . '</th>';
}

// Custom column content in the order "line items" html table
add_action( 'woocommerce_admin_order_item_values', 'custom_admin_order_item_values', 20, 3 );
function custom_admin_order_item_values( $_product, $item, $item_id ) {
    // Only for "line_item" items type
    if( ! $item->is_type('line_item') ) return;

    // For product variation, we get the parent variable product (in case of)
    if( $_product->is_type('variation') ){
        $parent_product = $_product->get_parent();
        // The product variation description (as short description doesn't exist)
        $excerpt        = $_product->get_description(); 
        // If product variation description doesn't exist we display the short description of the parent variable product
        $excerpt        = empty($excerpt) ? $parent_product->get_short_description() : $excerpt;
    }
    // For other product types
    else {
        $excerpt        = $_product->get_short_description();
    }

    // Output
    echo '<td class="line_custom-description">' . $excerpt . '</td>';
}

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

-1
votes

@LoicTheAztec answer does not work for me, i modified his answer and replace the custom_admin_order_item_values function with below, then it work.

// Custom column content in the order "line items" html table
function custom_admin_order_item_values( $item_name, $item, $is_visible ){
    $product_id = $item->get_product_id(); // Get the product Id
    $excerpt = get_the_excerpt( $product_id ); // Get the short description
    // Output
    echo '<td class="line_custom-description">' . $excerpt . '</td>';
}