Maged Mohamed
Maged Mohamed

Reputation: 561

Show woocommerce product description next to product name in orders deatils page at the backend and make it searchable

I need a help where i want to Show woocommerce product description next to product name in orders details page at the backend admin ONLY dashboard

So if product name is: Mobile Product Description: Type iPhone

So i want the result to be: Mobile Type iPhone, instead of Mobile

enter image description here

I tried to achieve and searched the internet but couldn't reach a destination.

I have achieved this by the code below:

add_filter( 'woocommerce_order_item_get_name', 'filter_order_item_get_name', 10, 2 );
function filter_order_item_get_name( $item_name, $order_item ) {
    if ( is_admin() && $order_item->is_type('line_item') ) {
        $product = $order_item->get_product();

        if( $description = $product->get_name() . " - " . $product->get_description() ) {
            $item_name = $description;
        }
    }
    return $item_name;
}

But still can't search by the product description as i want to search by the value of the product description itself not only the name to show the result of orders with this product description.

Upvotes: 1

Views: 1399

Answers (1)

JMRC
JMRC

Reputation: 1514

Unfortunately there is no hook to fix this and you can't easily overwrite admin templates. The intended way to do this is to use woocommerce_admin_order_item_headers from \wp-content\plugins\woocommerce\includes\admin\meta-boxes\views\html-order-item.php and add a column by echoing something like

add_action('woocommerce_admin_order_item_headers', function(){
    echo '<th class="sortable" data-sort="string-ins" style="max-width: 200px">Description</th>';
});

and use woocommerce_admin_order_item_values from \wp-content\plugins\woocommerce\includes\admin\meta-boxes\views\html-order-item.php to add the description in each row like

add_action('woocommerce_admin_order_item_values', function($product){
    if( $product )
        echo '<td style="max-width: 200px">'.esc_html($product->get_description()).'</td>';
});

If you really want to add it to the title, you'll probably be better of using JavaScript.

UPDATE

If you want to search for products with similar short descriptions, you can use tags. This code adds the tags to the item rows. If you click them you'll go to the archive page.

add_action('woocommerce_after_order_itemmeta', function($item_id, $item, $product){
    if( !$product )
        return;

    $tags = get_the_terms($product->get_id(), 'product_tag');
    if( $tags ){
        $html = '';
        foreach($tags as $tag)
            $html .= ($html ? ', ' : '').'<a href="/wp-admin/edit.php?post_type=product&product_tag='.esc_attr($tag->slug).'">'.esc_html($tag->name).'</a>';
        echo ($html ? 'tags: '.$html : $html);
    }
}, 10, 3);

Upvotes: 2

Related Questions