Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a Custom Field to WooCommerce Order Items

Currently, WooCommerce Order Items within an order, have the following columns:

  • Item
  • Tax Class
  • Qty
  • Totals
  • Tax

See screenshot

What I'm aiming to do is add an extra column (or meta) which has a dropdown field for Product Status.

Does anyone have any ideas on how I might accomplish this?

like image 456
Thomas McGee Avatar asked Dec 16 '22 03:12

Thomas McGee


2 Answers

In case someone else stumbles upon this, I needed to add some custom order meta to the order items meta box on the edit order screen. In 2017 this is how I solved this dilemma.

The file class-wc-meta-box-order-items.php, found under includes/admin/meta-boxes has changed slightly since 2014 but it does include the template file html-order-items.php.

It's in that last file that you will find two undocumented hooks, woocommerce_admin_order_item_headers, which you would use to add your custom column heading text and has access to the $order object and woocommerce_admin_order_item_values, which places your custom content right before the Cost column and has access to $product, $item and $item_id.

So to add a custom column it would look something like this.

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">
    Custom Title
  </th>
  <?php
}

where your-sort-option depends on what data you are wanting to sort. I used string-ins in my situation.

Than for the content in each line item you would have.

add_action( 'woocommerce_admin_order_item_values', 'pd_admin_order_item_values' );
function pd_admin_order_item_values( $product, $item, $item_id ) {
  //Get what you need from $product, $item or $item_id
  ?>
  <td class="line_customtitle">
    <?php //your content here ?>
  </td>
  <?php
}

There are a few other hooks and filters in that template file that are definitely worth looking at if you need content in different places within that meta-box.

like image 124
Paul Dowlin Avatar answered Jan 03 '23 20:01

Paul Dowlin


I am in the middle of some major adjustments to this table as well, and I haven't figured it all out yet, but I know this much, if you review the class-wc-meta-box-order-items.php within the plugin directory, you will find this snippet of code:

// List order items
$order_items = $order->get_items( apply_filters( 'woocommerce_admin_order_item_types',  array( 'line_item', 'fee' ) ) );

foreach ( $order_items as $item_id => $item ) {

switch ( $item['type'] ) {
    case 'line_item' :
        $_product   = $order->get_product_from_item( $item );
        $item_meta  = $order->get_item_meta( $item_id );

        include( 'views/html-order-item.php' );
    break;
    case 'fee' :
        include( 'views/html-order-fee.php' );
    break;
}

do_action( 'woocommerce_order_item_' . $item['type'] . '_html', $item_id, $item );
}

DO NOT EDIT THIS SNIPPET!!!

From here you can begin to see what makes up that table. I believe (from what I know of WooCommerce so far) is you can create a function that hooks in at the filter there: woocommerce_admin_order_item_types();

That's what I would do something like this:

# Admin Panel Updates
add_filter( 'woocommerce_add_order_item_meta', array( $this, 'display_order_item_meta' ), 10, 2 );
public function display_order_item_meta( $order_items ) {
    array_push($order_items,'event'); //Not sure how to manipulate this yet
    return $order_items;
}

Also it looks like we would need to do something with the action hook (woocommerce_order_item_' . $item['type'] . '_html', $item_id, $item), perhaps something along these lines:

add_filter( 'woocommerce_order_item_event_html', array( $this, 'display_event_item_meta' ), 10, 2 );

public function display_event_item_meta( $item_id, $item) {
    switch ( $item['type'] ) {
        case 'event' :
        include( 'views/html-order-event-item.php' );
            break;
    }
}

That's my best guess so far, but I'm positive this is the right snippet of code to be dissecting.

like image 25
Xtremefaith Avatar answered Jan 03 '23 20:01

Xtremefaith