Content Drivers

  1. # Configuration
  2. # Example

Simple Commerce has a concept of 'content drivers'. You can use content drivers to switch out how/where 'things' are stored.

Essentially, a content driver is a class which implements a certain interface. The class has all of the methods needed to allow for Simple Commerce to do its job.

You can configure content drivers for the following:

  • Products
  • Orders
  • Coupons
  • Customers

Configuration

1'content' => [
2 'orders' => [
3 'driver' => \DoubleThreeDigital\SimpleCommerce\Orders\Order::class,
4 'collection' => 'orders',
5 ],
6
7 'products' => [
8 'driver' => \DoubleThreeDigital\SimpleCommerce\Products\Product::class,
9 'collection' => 'products',
10 ],
11
12 'coupons' => [
13 'driver' => \DoubleThreeDigital\SimpleCommerce\Coupons\Coupon::class,
14 'collection' => 'coupons',
15 ],
16
17 'customers' => [
18 'driver' => \DoubleThreeDigital\SimpleCommerce\Customers\Customer::class,
19 'collection' => 'customers',
20 ],
21],

As you can see, you can assign a driver class to each of the available driver types. You may also provide any additional configuration values, like collection, which can be used by the chosen driver.

Low-level rundown

For those interested: here's a low-level rundown of how content drivers work:

Whenever a Simple Commerce facade is used, it looks for a binding in the service container attached to the relevant interface/contract (DoubleThreeDigital\SimpleCommerce\Contracts\Product).

When Simple Commerce is booted, it will automatically register a binding between the Simple Commerce interface and the chosen content driver.

Example

Here's a bare bones example to get you started:

1<?php
2
3namespace App\SimpleCommerce;
4
5use DoubleThreeDigital\SimpleCommerce\Contracts\Order as Contract;
6use DoubleThreeDigital\SimpleCommerce\Support\Traits\HasData;
7use Illuminate\Support\Collection;
8
9class OrderRepository implements Contract
10{
11 use HasData;
12
13 public $id;
14 public $site;
15 public $title;
16 public $slug;
17 public $data;
18 public $published;
19
20 /** @var \Illuminate\Database\Eloquent\Model $model */
21 protected $model;
22
23 public function all()
24 {
25 //
26 }
27
28 public function query()
29 {
30 //
31 }
32
33 public function find($id): self
34 {
35 //
36
37 return $this;
38 }
39
40 public function create(array $data = [], string $site = ''): self
41 {
42 //
43
44 return $this;
45 }
46
47 public function save(): self
48 {
49 //
50
51 return $this;
52 }
53
54 public function delete()
55 {
56 //
57 }
58
59 public function toResource()
60 {
61 //
62 }
63
64 public function id()
65 {
66 return $this->id;
67 }
68
69 public function title($title = '')
70 {
71 //
72 }
73
74 public function slug($slug = '')
75 {
76 //
77 }
78
79 public function site($site = null)
80 {
81 //
82 }
83
84 public function fresh(): self
85 {
86 //
87
88 return $this;
89 }
90
91 public function set(string $key, $value)
92 {
93 //
94
95 return $this;
96 }
97
98 public function toArray(): array
99 {
100 return $this->model->toArray();
101 }
102
103 public function billingAddress()
104 {
105 //
106 }
107
108 public function shippingAddress()
109 {
110 //
111 }
112
113 public function customer($customer = '')
114 {
115 //
116 }
117
118 public function coupon($coupon = '')
119 {
120 //
121 }
122
123 public function redeemCoupon(string $code): bool
124 {
125 //
126
127 return true;
128 }
129
130 public function markAsCompleted(): self
131 {
132 //
133
134 return $this;
135 }
136
137 public function buildReceipt(): string
138 {
139 return 'blah blah blah';
140 }
141
142 public function calculateTotals(): self
143 {
144 //
145
146 return $this;
147 }
148
149 public function lineItems(): Collection
150 {
151 return collect();
152 }
153
154 public function lineItem($lineItemId): array
155 {
156 //
157
158 return [];
159 }
160
161 public function addLineItem($lineItemData): array
162 {
163 //
164
165 return [];
166 }
167
168 public function updateLineItem($lineItemId, array $lineItemData): array
169 {
170 //
171
172 return [];
173 }
174
175 public function removeLineItem($lineItemId): Collection
176 {
177 //
178
179 return collect();
180 }
181
182 public function clearLineItems(): Collection
183 {
184 //
185
186 return collect();
187 }
188
189 public static function bindings(): array
190 {
191 return [];
192 }
193}
Last updated 1 month ago Improve this page on GitHub!