How To Create A Shipping Module

Title

No changes

Permalink

No changes

Content

OldNew
1# How To Create A Shipping Module1# How To Create A Shipping Module
2
3## A Basic Example
4
5**/includes/modules/shipping/sm_my_shipping_module.inc.php>:**
6```php
27
3## A Basic Example8<?php
49
5```php /includes/modules/shipping/sm_my_shipping_module.inc.php10 class sm_my_shipping_module {
11 public $id = __CLASS__;
12 public $name = 'My Shipping Module';
13 public $description = '';
14 public $author = 'ACME Corp.';
15 public $version = '1.0';
16 public $website = 'http://www.acme.com';
17
18 public function options($items, $subtotal, $tax, $currency_code, $customer) {
19
20 if (empty($this->settings['status'])) return;
21
22 return [
23 'title' => $this->name,
24 'options' => [
25 [
26 'id' => 'option_1',
27 'icon' => $this->settings['icon'],
28 'name' => 'Option 1',
29 'description' => 'This is the description for option 1',
30 'fields' => '',
31 'cost' => $this->settings['fee_1'],
32 'tax_class_id' => $this->settings['tax_class_id'],
33 'incoterm' => $this->settings['incoterm'],
34 'exclude_cheapest' => false,
35 'error' => '',
36 ],
37 [
38 'id' => 'option_2',
39 'icon' => $this->settings['icon'],
40 'name' => 'Option 2',
41 'description' => 'This is the description for option 2',
42 'fields' => '',
43 'cost' => $this->settings['fee_2'],
44 'tax_class_id' => $this->settings['tax_class_id'],
45 'incoterm' => $this->settings['incoterm'],
46 'exclude_cheapest' => false,
47 'error' => '',
48 ],
49 ],
50 ];
51 }
52
53 public function settings() {
54 return [
55 [
56 'key' => 'status',
57 'default_value' => '1',
58 'title' => language::translate(__CLASS__.':title_status', 'Status'),
59 'description' => language::translate(__CLASS__.':description_status', ''),
60 'function' => 'toggle("e/d")',
61 ],
62 [
63 'key' => 'icon',
64 'default_value' => '',
65 'title' => language::translate(__CLASS__.':title_icon', 'Icon'),
66 'description' => language::translate(__CLASS__.':description_icon', 'Web path of the icon to be displayed.'),
67 'function' => 'text()',
68 ],
69 [
70 'key' => 'fee_1',
71 'default_value' => '10',
72 'title' => 'Fee 1',
73 'description' => 'This is the fee for option 1',
74 'function' => 'decimal()',
75 ],
76 [
77 'key' => 'fee_2',
78 'default_value' => '10',
79 'title' => 'Fee 2',
80 'description' => 'This is the fee for option 2',
81 'function' => 'decimal()',
82 ],
83 [
84 'key' => 'tax_class_id',
85 'default_value' => '',
86 'title' => language::translate(__CLASS__.':title_tax_class', 'Tax Class'),
87 'description' => language::translate(__CLASS__.':description_tax_class', 'The tax class for the shipping cost.'),
88 'function' => 'tax_class()',
89 ],
90 [
91 'key' => 'incoterm',
92 'default_value' => '',
93 'title' => language::translate(__CLASS__.':title_incoterm', 'Incoterm'),
94 'description' => language::translate(__CLASS__.':description_incoterm', 'The incoterm set by this module.'),
95 'function' => 'incoterm()',
96 ],
97 [
98 'key' => 'priority',
99 'default_value' => '0',
100 'title' => language::translate(__CLASS__.':title_priority', 'Priority'),
101 'description' => language::translate(__CLASS__.':description_priority', 'Process this module by the given priority value.'),
102 'function' => 'number()',
103 ],
104 ];
105 }
106 }
107```
108
109## Class Methods
110
111### options($items, $subtotal, $tax, $currency_code, $customer)
112
113This method is called by the checkout page requesting available shipping options.
114
115If you need to collect some additional data, add your custom input fields to the 'fields' element. They can then be used in a later event in the shipping module.
116
117```php
118 public function options($items, $subtotal, $tax, $currency_code, $customer) {
119 return [
120 'title' => $this->name,
121 'options' => [
122 [
123 'id' => 'option_1',
124 'icon' => $this->settings['icon'],
125 'name' => 'Option 1',
126 'description' => 'This is the description for option 1',
127 'fields' => '<input type="text" name="foo" value="'. @$this->userdata['foo'] .'" />' . PHP_EOL
128 . '<input type="text" name="bar" value="'. @$this->userdata['bar'] .'" />',
129 'cost' => $this->settings['fee_1'],
130 'tax_class_id' => $this->settings['tax_class_id'],
131 'incoterm' => $this->settings['incoterm'],
132 'exclude_cheapest' => false,
133 ],
134 ],
135 ];
136 }
137```
138
139### select($module_id, $option_id)
140
141The method is triggered upon an option of the module being selected, making it possible to verify any input data.
142
143```php
144 public function select($module_id, $option_id) {
145 // Do something
146 // ...
147 }
148```
149
150### after_process($order)
151
152This method is triggered after the order has been created, making it possible to automate e.g. booking of shipping.
153
154```php
155 public function after_process($order) {
156 // Do something
157 // ...
158
159 // Save order
160 $order->save();
161 }
162```
163
164### install()
165
166The method is triggered upon installing the module. Good for creating necessary database tables or such.
167
168```php
169 public function install() {
170 // Do something
171 // ...
172 }
173```
174
175### uninstall()
176
177The method is triggered upon uninstalling the module. Good for cleaning up database tables and such.
178
179```php
180 public function uninstall() {
181 // Do something
182 // ...
183 }
184```
185### update()
186
187The method is triggered upon changing the module configuration. Good for updating database tables and such.
188```php
189 public function update() {
190 // Do something
191 // ...
192 }
193```
194### settings()
195
196This method returns a settings structure with editable parameters. Mandatory parameters are status, icon and priority.
197
198```php
199 public function settings() {
200 return [
201 [
202 'key' => 'status',
203 'default_value' => '1',
204 'title' => language::translate(__CLASS__.':title_status', 'Status'),
205 'description' => language::translate(__CLASS__.':description_status', ''),
206 'function' => 'toggle("e/d")',
207 ],
208 [
209 'key' => 'icon',
210 'default_value' => '',
211 'title' => language::translate(__CLASS__.':title_icon', 'Icon'),
212 'description' => language::translate(__CLASS__.':description_icon', 'Web path of the icon to be displayed.'),
213 'function' => 'text()',
214 ],
215 [
216 'key' => 'weight_class',
217 'default_value' => '',
218 'title' => language::translate(__CLASS__.':title_weight_class', 'Weight Class'),
219 'description' => language::translate(__CLASS__.':description_weight_class', 'The weight class for the rate table.'),
220 'function' => 'weight_class()',
221 ],
222 [
223 'key' => 'geo_zone_id',
224 'default_value' => '',
225 'title' => language::translate(__CLASS__.':title_zone', 'Zone') . language::translate(__CLASS__.':title_geo_zone', 'Geo Zone'),
226 'description' => language::translate(__CLASS__.':description_geo_zone', 'Geo zone to which the cost applies.'),
227 'function' => 'geo_zone()',
228 ],
229 [
230 'key' => 'rate_table',
231 'default_value' => '5:8.95;10:15.95',
232 'title' => language::translate(__CLASS__.':title_rate_table', 'Rate Table'). language::translate(__CLASS__.':title_rate_table', 'Rate Table'),
233 'description' => language::translate(__CLASS__.':description_rate_table', 'Ascending rate table of the shipping cost. The format must be weight:cost;weight:cost;.. (I.e. 5:8.95;10:15.95;..)'),
234 'function' => 'text()',
235 ],
236 [
237 'key' => 'tax_class_id',
238 'default_value' => '',
239 'title' => language::translate(__CLASS__.':title_tax_class', 'Tax Class'),
240 'description' => language::translate(__CLASS__.':description_tax_class', 'The tax class for the shipping cost.'),
241 'function' => 'tax_class()',
242 ],
243 [
244 'key' => 'incoterm',
245 'default_value' => '',
246 'title' => language::translate(__CLASS__.':title_incoterm', 'Incoterm'),
247 'description' => language::translate(__CLASS__.':description_incoterm', 'The incoterm set by this module.'),
248 'function' => 'incoterm()',
249 ],
250 [
251 'key' => 'priority',
252 'default_value' => '0',
253 'title' => language::translate(__CLASS__.':title_priority', 'Priority'),
254 'description' => language::translate(__CLASS__.':description_priority', 'Process this module by the given priority value.'),
255 'function' => 'number()',
256 ],
257 ];
258 }
259```

Edited by s22_tech on Jan 4 2024 at 12:31 AM