Describe your bug or feature request.

Currently the BaseFieldDefinitions of the OrderItem entity do not make it possible to add the adjustments to the Form Mode.

This means that as an admin, it's not possible to see or edit adjustments applied at the Line Item level.

$fields['adjustments'] = BaseFieldDefinition::create('commerce_adjustment')
  ->setLabel(t('Adjustments'))
  ->setRequired(FALSE)
  ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  ->setDisplayConfigurable('form', FALSE)
  ->setDisplayConfigurable('view', TRUE);

If a bug, provide steps to reproduce it from a clean install.

1. Create OrderItem, add just Adjustment, add to cart


$orderItem = OrderItem::create([
  'type' => 'default',
  'quantity' => 1,
  'data' => [],
  'purchased_entity' => $product,
  'title' => 'Test',
  'unit_price' => $product->getPrice(),
]);

$adjustment = new Adjustment([
  'type' => 'custom',
  'amount' => new Price('10', 'GBP'),
  'label' => $this->t('Test Adjustment'),
  'source_id' => 'my_custom_source_id',
  'included' => FALSE,
  'locked' => TRUE,
]);

$orderItem->addAdjustment($adjustment);
$orderItem->save();

$this->cartManager->addOrderItem($cart, $orderItem, FALSE);

2. View cart in the Admin, there's no option to modify to add adjustments at the line item level, and no option to add the adjustments field to the UI.

Issue

Issue

It seems like it would be fairly easy just to change this field to allow Developers to place it on the Edit screens if desired?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mattjones86 created an issue. See original summary.

mattjones86’s picture

For anyone else that needs to solve this, here's how I've done it:

/**
 * Allow adjustments to be edited via the admin
 *
 * Implements hook_entity_base_field_info_alter().
 */
function mymodule_entity_base_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'commerce_order_item' && !empty($fields['adjustments'])) {
    $fields['adjustments']->setDisplayConfigurable('form', TRUE);
  }
}
jsacksick’s picture

Probably doesn't hurt to make this change in core without displaying it by default so anyone wanting to do this could.

jsacksick’s picture

Status: Needs work » Needs review
FileSize
925 bytes

  • jsacksick committed 0d3bd02 on 8.x-2.x
    Issue #3231306 by mattjones86, jsacksick: Allow the order item...
jsacksick’s picture

Status: Needs review » Fixed

Went ahead and committed the fix, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

mattjones86’s picture

Great, thanks!