Store recipes

Everything starts with a store. Products can belong to many stores, and orders belong to a single store.

Creating a store type

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/**
 * id [String]
 *   The primary key for this store type.
 *
 * label [String]
 *   The label for this store type.
 *
 * description [String]
 *   The description for this store type.
 */
$store_type = \Drupal\commerce_store\Entity\StoreType::create([
  'id' => 'custom_store_type',
  'label' => 'My custom store type',
  'description' => 'This is my first custom store type!',
]);
$store_type->save();

Loading a store type

1
2
// Loading is based off of the primary key [String] that was defined when creating it.
$store_type = \Drupal\commerce_store\Entity\StoreType::load('custom_store_type');

Creating a store

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
 * type [String] - [DEFAULT = 'online']
 *   Foreign key for the store type to yse.
 *
 * uid [Integer]
 *   The user id that created the store.
 *
 * name [String]
 *   The store's name.
 *
 * mail [String]
 *   The store's email address.
 *
 * address [\Drupal\address\AddressInterface]
 *   The store's address.
 *
 * default_currency [String]
 *   The currency the store uses.
 *
 * billing_countries [Array(String)]
 *   Array of country codes selected for the store.
 */

// The store's address.
$address = [
  'country_code' => 'US',
  'address_line1' => '123 Street Drive',
  'locality' => 'Beverly Hills',
  'administrative_area' => 'CA',
  'postal_code' => '90210',
];

// The currency code.
$currency = 'USD';

// If needed, this will import the currency.
$currency_importer = \Drupal::service('commerce_price.currency_importer');
$currency_importer->import($currency);

$store = \Drupal\commerce_store\Entity\Store::create([
  'type' => 'custom_store_type',
  'uid' => 1,
  'name' => 'My Store',
  'mail' => 'admin@example.com',
  'address' => $address,
  'default_currency' => $currency,
  'billing_countries' => ['US'],
]);
$store->save();

// If needed, this sets the store as the default store.
$store_storage = \Drupal::service('entity_type.manager')->getStorage('commerce_store');
$store_storage->markAsDefault($store);

Loading a store

1
2
3
// Loading is based off of the primary key [Integer]
//   1 would be the first one saved, 2 the next, etc.
$store = \Drupal\commerce_store\Entity\Store::load(1);