Automatically set carrier when shipment is created

Created on 19 May 2022, over 2 years ago
Updated 17 March 2023, over 1 year ago

Problem/Motivation

Currently setting the carrier is manually done by a human during creation/editing of a shipment.

Proposed resolution

Shipping methods should have an option to define a carrier and when that method is used for a shipment the carrier is automatically set. This will allow the carrier to automatically be set during customer checkout.

Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

🇺🇸United States rhovland Oregon

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇪Germany 4kant

    two questions that likely/probably fit into this issue:

    1. In checkout the customer can select the carrier. If that´s intentional: that should be hidden in my case --> I have only UPS so far
    2. Maybe at least a "default" carrier could be set automatically

    Anyway - thanks for the module!

  • In checkout the customer can select the carrier. If that´s intentional: that should be hidden in my case --> I have only UPS so far

    The carrier field can be hidden in the shipment type field display.

    Maybe at least a "default" carrier could be set automatically

    The default carrier could be set in the shipping method, and then we would need to set the shipping carrier on the shipment:
    $shipment->set('shipping_carrier', $shipping_carrier);

    Again, I would be glad to review a patch.

  • Adding this to the module file gets our field on the shipping method:

      if ($entity_type->id() === 'commerce_shipping_method') {
        $fields['shipping_carrier'] = BaseFieldDefinition::create('entity_reference')
          ->setLabel(t('Shipping carrier'))
          ->setCardinality(1)
          ->setRequired(FALSE)
          ->setDescription(t('The shipping carrier'))
          ->setSetting('target_type', 'commerce_shipping_carrier')
          ->setDisplayOptions('form', [
            'type' => 'options_select',
            'weight' => 0,
          ])
          ->setDisplayConfigurable('form', TRUE)
          ->setDisplayConfigurable('view', TRUE);
      }

    Plugin would probably be better, but I don't really have time to figure out how to do that. :)

  • Status changed to Needs review over 1 year ago
  • Here is a sloppy patch to test the idea. You will need to run drush entup with devel_entity_updates, or something like that to get the field.

  • Status changed to Active over 1 year ago
  • Not really sure I want to hard code this. Carrier module already provides the field, and shipping methods are fieldable, but there is no UI.

    See https://www.drupal.org/project/commerce_shipping/issues/3276238

    Ideally, users could add the field to the shipping method, and implement their own logic in an event subscriber.

  • Status changed to Needs review over 1 year ago
  • On second thought, adding the field to the shipping method field data seems harmless, as that will be removed if the carrier module is uninstalled. This would at least make it easier for people who want to add subscribers.

    Here is a little cleaner patch. Might rethink the name of the subscriber that ships with the module.

Production build 0.71.5 2024