2

I have a list of shipping methods added via the UI admin/commerce/config/shipping-methods.

I need to have an accompanying description added to each shipping method.
First I thought that I could add an additional field under the settings for shipping types, but this didn't work (Added the field, but it never shows up when adding or editing a shipping method).

I'm fine if this is only doable programmatically, but I have found absolute zero information on how to do this on drupal 8.
I would actually prefer if I can do it programmatically, as I will be adding my own shipping method plugin in the future.

Question
How do I add an additional field to each shipping method?

1 Answer 1

2

I know this is an old thread, but I encountered the same problem. So in case you have the same issue:

This isn't really a commerce question, rather a general one.

There is a hook, where you can simple add new fields to an existing entity: HOOK_entity_base_field_info

So in this case:

/**
 * Implements hook_entity_base_field_info().
 */
function yourmodule_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'commerce_shipping_method') {
    $fields['shipment_description'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Description'))
      ->setDescription(t('The shipping method description.'))
      ->setRequired(FALSE)
      ->setTranslatable(TRUE)
      ->setSettings([
        'default_value' => '',
        'max_length' => 255,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textarea',
        'weight' => 0,
      ])
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE);
    }
}

This will add a simple textarea field type, but you can add any field type you like.

This will only add the field visually, you must also do an update to your database table. In a .install file, do something like this:

/**
 * Implements hook_install().
 */
function hartmann_add_field_install() {
  $entity_manager = \Drupal::entityManager();

  $desc_definition = $entity_manager->getFieldStorageDefinitions('commerce_shipping_method')['shipment_description'];
  $entity_manager->onFieldStorageDefinitionCreate($desc_definition);
}

/**
 * Implements hook_uninstall().
 */
function hartmann_add_field_uninstall() {
  $entity_manager = \Drupal::entityManager();

  $desc_definition = $entity_manager->getLastInstalledFieldStorageDefinitions('commerce_shipping_method')['shipment_description'];
  $entity_manager->onFieldStorageDefinitionDelete($desc_definition);
}

Then reinstall your module. In more recent versions of Drupal 8, entitymanager is deprecated. I don't now the updated code for .install, but some research and you will find it.

1
  • 1
    This is indeed what we ended up doing, with the 8.7 update that removed the automated entity updates it just has to be done a little differently, information on how to do that can be found her: drupal.org/node/3034742 Commented Aug 29, 2019 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.