How can I programatically add a shipping method to a store? I am using Commerce with Marketplace, and am using the Shipping module.
In my commerce marketplace, users become sellers via a custom form, where I create the store entity on submit.  Currently, to attach a shipping method, it has to be done in commerce admin via a checkbox:

My marketplace can have an unlimited number of stores, making management of them via checkboxes unfeasible.  How can I add a shipping method to a store?  There's a table commerce_shipping_method__stores:

Can I simply make an insert into this table and set the stores_target_id value to the new store id after the new store is created?
With custom module code, you can add a new store like this:
function mymodule_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {

  if ($entity instanceof Drupal\commerce_store\Entity\StoreInterface) {

      $sm_id = 1; // I can see Shipping Method ID is 1 from your table screenshot
      $shipping_method = Drupal\commerce_shipping\Entity\ShippingMethod::load($sm_id);
      $store_ids = $shipping_method-&gt;getStoreIds(); // get existing IDs
      $new_store_id = $entity-&gt;id(); // get new id
      $store_ids[] = $new_store_id; // add new to existing
      $shipping_method-&gt;setStoreIds($store_ids); // set
      $shipping_method-&gt;save();

  }

}

In the above code, when a store is created, it automatically adds it to the shipping method.


But if your question is to add already existing stores to the shipping method, then run this code only once:
      $sm_id = 1; // I can see Shipping Method ID is 1 from your table screenshot
      $shipping_method = Drupal\commerce_shipping\Entity\ShippingMethod::load($sm_id);
      $store_ids = [1, 2, 3, 4, 5, 6]; // Array of all the store IDs that belong to this shipping method.
      $shipping_method-&gt;setStoreIds($store_ids); // set
      $shipping_method-&gt;save();


The better API solution
Thanks to 4k4's answer I was able to commeup with a more efficient solution.
function mymodule_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {

  if ($entity instanceof Drupal\commerce_store\Entity\StoreInterface) {

      $sm_id = 1; // I can see Shipping Method ID is 1 from your table screenshot
      $shipping_method = Drupal\commerce_shipping\Entity\ShippingMethod::load($sm_id);
      $new_store_id = $entity-&gt;id(); // get new id
      $shipping_method-&gt;{'stores'}[] = $new_store_id; // appends new store to existing list.
      $shipping_method-&gt;save();

  }

}


Manual Solution

Quick follow up, do I have to load all the stores at once? When I have
1000+ stores and more, that seems like it will be a little resource
intensive. Is it possible to just add the new store?

At this point, you wouldn't want to use the entity API and do Drupal\commerce_shipping\Entity\ShippingMethod::load as this would contain all the 1000+ store IDs, you will have to manually DB query &amp; insert:
function mymodule_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {

  if ($entity instanceof Drupal\commerce_store\Entity\StoreInterface) {

    $sm_id = 1; // Shipping Method ID
    $connection = \Drupal::database();

    $query = $connection-&gt;select('commerce_shipping_method__stores', 'csms');
    $query-&gt;range(0, 1)
      -&gt;orderBy('delta', 'DESC')
      -&gt;condition('csms.entity_id', $sm_id)
      -&gt;addField('csms', 'delta');
    $last_delta = $query-&gt;execute()-&gt;fetchField();
    $new_delta = $last_delta + 1;
    $new_store_id = $entity-&gt;id(); // new store ID

    $insert = $connection-&gt;insert('commerce_shipping_method__stores')
      -&gt;fields([
        'entity_id' =&gt; $sm_id,
        'stores_target_id' =&gt; $new_store_id,
        'revision_id' =&gt; $sm_id,
        'delta' =&gt; $new_delta,
        'bundle' =&gt; 'commerce_shipping_method',
        'langcode' =&gt; 'en',
      ])
      -&gt;execute();

    $storage = \Drupal::entityTypeManager()
      -&gt;getStorage('commerce_shipping_method');
    $storage-&gt;resetCache([$sm_id]); // Flush Cache

  }

}