I mentioned this above in a comment, but the reason this takes more work than editing a View is because there's no simple way for a field widget to use Views to build part of a form. You can easily use Views to render content, and you can even use Views to construct a complete form, but using it to embed part of a form inside another form is not likely to happen.
There is a more straightforward way to customize the table if you're interested, based on the fact that the Inline Entity Form module gives you two ways to override the fields shown in the table of its "complex" widget.
If you anticipate making more holistic changes to the behavior of the inline form, you might consider setting a custom inline_form handler for order items via hook_entity_type_alter():
use Drupal\custom\Form\CustomOrderItemInlineForm;
/**
* Implements hook_entity_type_alter().
*/
function custom_entity_type_alter(&$entity_types) {
$entity_types['commerce_order_item']->setHandlerClass('inline_form', CustomOrderItemInlineForm::class);
}
Your handler class would inherit the default OrderItemInlineForm class and override its getTableFields() method to return a different fields array that adds your SKU field in the right place:
namespace Drupal\custom\Form;
use Drupal\commerce_order\Form\OrderItemInlineForm;
/**
* Defines the inline form for order items.
*/
class CustomOrderItemInlineForm extends OrderItemInlineForm {
/**
* {@inheritdoc}
*/
public function getTableFields($bundles) {
$fields = parent::getTableFields($bundles);
$fields['sku'] = [
'type' => 'callback',
'callback' => 'custom_order_item_sku',
'label' => t('SKU'),
'weight' => 2,
];
$fields['unit_price']['weight'] = 3;
$fields['quantity']['weight'] = 4;
return $fields;
}
}
Alternately, you can just implement hook_inline_entity_form_table_fields_alter() to do the same thing:
/**
* Implements hook_inline_entity_form_table_fields_alter().
*/
function custom_inline_entity_form_table_fields_alter(&$fields, $context) {
// Note: you'd want to check the context here to only alter the desired table.
$fields['sku'] = [
'type' => 'callback',
'callback' => 'custom_order_item_sku',
'label' => t('SKU'),
'weight' => 2,
];
$fields['unit_price']['weight'] = 3;
$fields['quantity']['weight'] = 4;
}
Note that when you define the field for the Inline Entity Form table to include, since the SKU doesn't come from the order item itself (which is what is rendered in that table), you can't just use the 'field' type - you have to define a custom callback. Your callback is a simple function that can load the purchased entity from the order item, confirm it has a SKU, and then return it for display:
/**
* Returns the SKU for the product referenced by an order item.
*/
function custom_order_item_sku($entity, $variables) {
// Extract the purchased entity from the order item.
$purchased_entity = $entity->getPurchasedEntity();
// If the purchased entity couldn't be loaded or doesn't have a SKU...
if (!$purchased_entity || !$purchased_entity->hasField('sku')) {
// Return a placeholder string.
return '-';
}
else {
// Otherwise, return the SKU itself.
return $purchased_entity->getSku();
}
}
I tested this myself locally and it worked like a charm. : )