Search code examples
wordpresswoocommerceordersskuwordpress-admin

WooCommerce: Display SKU number in admin side Order listing page custom column


I have added a custom column in Woocommerce admin side order listing page to display Ordered item Products name and total Quantity like this -> [http://prntscr.com/p11rdl] by using the following code:

add_filter('manage_edit-shop_order_columns', 'misha_order_items_column' );
function misha_order_items_column( $order_columns ) {
    $order_columns['order_products'] = "Qty-Products Name-SKU";
    return $order_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
function misha_order_items_column_cnt( $colname ) {
  global $the_order; // the global order object
  if( $colname == 'order_products' ) {
    // get items from the order global object
    $order_items = $the_order->get_items();
    if ( !is_wp_error( $order_items ) ) {
      foreach( $order_items as $order_item ) {
        echo $order_item['quantity'] .' × <a href="' . admin_url('post.php?post=' . $order_item['product_id'] . '&action=edit' ) . '" target="_blank">'. $order_item['name'] .'</a><br />';
      }
    }
  } 
}

Now I also want to display ordered product SKU number after the product name. So anyone have solution for this then please provide help me.

Thank you,
Ketan


Solution

  • Just replace with follows code snippets to add SKU -

    add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt' );
    function misha_order_items_column_cnt( $colname ) {
      global $the_order; // the global order object
      if( $colname == 'order_products' ) {
        // get items from the order global object
        $order_items = $the_order->get_items();
        if ( !is_wp_error( $order_items ) ) {
          foreach( $order_items as $order_item ) {
            $product = $order_item->get_product();
            // product checking
            $sku = ( $product && $product->get_sku() ) ? ' - ' . $product->get_sku() : '';
            echo $order_item['quantity'] .' × <a href="' . admin_url('post.php?post=' . $order_item['product_id'] . '&action=edit' ) . '" target="_blank">'. $order_item['name'] . '</a>'. $sku . '<br />';
          }
        }
      } 
    }