commerce_shipping.install
9.3 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* @file Contains updade hooks for the commerce_shipping module.
*/
/**
* Implements hook_schema().
*/
function commerce_shipping_schema() {
$schema = array();
$schema['cache_commerce_shipping_rates'] = drupal_get_schema_unprocessed('system', 'cache');
$schema['cache_commerce_shipping_rates']['description'] = 'Cache table for the temporary storage of base calculated shipping rates for orders.';
$schema['cache_commerce_shipping_rates']['fields']['cid']['description'] = 'Primary Key: Order ID and shipping method the rates are for.';
return $schema;
}
/**
* Implements hook_uninstall().
*/
function commerce_shipping_uninstall() {
// Delete shipping rules.
$rules = rules_config_load_multiple(FALSE);
foreach ($rules as $rule) {
if (strpos($rule->name, 'commerce_shipping') === 0) {
rules_config_delete(array($rule->id));
}
}
}
/**
* Add address field for the commerce_shipping customer profile.
*/
function commerce_shipping_update_7000(&$sandbox) {
$profile_types = commerce_shipping_commerce_customer_profile_type_info();
commerce_customer_configure_customer_profile_type($profile_types['shipping']);
return t('Address field added to commerce shipping customer profile.');
}
/**
* Add default profile reference field on order if needed
*/
function commerce_shipping_update_7001(&$sandbox) {
$profile_types = commerce_shipping_commerce_customer_profile_type_info();
$profile_type = $profile_types['shipping'];
commerce_order_configure_customer_profile_type($profile_type['type'], $profile_type['name']);
return t('Commerce customer profile field added to commerce order.');
}
/**
* Remove all old shipping rules and convert plugin type shipping rules to use
* the new naming convention.
*/
function commerce_shipping_update_7002(&$sandbox) {
$rules = rules_config_load_multiple(FALSE);
$deleted = 0;
$renamed = 0;
foreach ($rules as $rule) {
if ($rule->module == 'commerce_shipping') {
if (strpos($rule->name, 'commerce_shipping_plugin') !== FALSE) {
rules_config_delete(array($rule->id));
$deleted += 1;
}
}
}
$rules_actions_to_rename = array();
foreach ($rules as $rule) {
if ($rule->module == 'commerce_shipping' && strpos($rule->name, 'commerce_shipping_plugin') === FALSE) {
$rule->name = str_replace('commerce_shipping_plugin', 'commerce_shipping', $rule->name);
$rules_actions_to_rename[] = $rule->name;
foreach ($rule->actions() as $action) {
if (!empty($action->settings['shipping_method'])) {
$action->settings['shipping_method'] = str_replace('plugin-', '', $action->settings['shipping_method']);
$action->save();
}
}
$rule->save();
$renamed += 1;
}
}
// Rules doesn't provide any APIs to change the elementName of rule actions, so we modify them directly in the database.
$result = db_select('rules_config', 'r')
->fields('r', array('data', 'id'))
->condition('r.name', $rules_actions_to_rename, 'IN')
->execute();
foreach ($result as $row) {
preg_match('/(\d+):"commerce_shipping_enable_plugin-/', $row->data, $matches);
if (!empty($matches[1])) {
$replace = $matches[1] - 7;
$data = preg_replace('/\d+(:"commerce_shipping_enable_)plugin-/', $replace . '$1' , $row->data);
db_update('rules_config')
->fields(array('data' => $row->data))
->condition('id', $row->id)
->execute();
}
}
return t('Deleted @deleted shipping and renamed @renamed shipping rules', array('@deleted' => $deleted, '@renamed' => $renamed));
}
/**
* Rename the shipping_method field on the shipping line item to commerce_shipping_method.
* This should actually never be run on 2.x but is here for reference.
*/
function commerce_shipping_update_7003(&$sandbox) {
/*
commerce_shipping_line_item_configuration(array('type' => 'shipping'));
foreach (commerce_line_item_load_multiple(FALSE, array('type' => 'shipping')) as $line_item) {
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$line_item_wrapper->commerce_shipping_method = $line_item_wrapper->shipping_method->value();
$line_item_wrapper->save();
}
field_delete_field('shipping_method');
*/
}
/**
* Install the shipping rate cache table for Shipping 2.x if necessary.
* This should actually never be run on 2.x but is here for reference.
*/
function commerce_shipping_update_7004($sandbox) {
/*
if (!db_table_exists('cache_commerce_shipping_rates')) {
$table = drupal_get_schema_unprocessed('system', 'cache');
$table['description'] = 'Cache table for the temporary storage of base calculated shipping rates for orders.';
$table['fields']['cid']['description'] = 'Primary Key: Order ID and shipping method the rates are for.';
db_create_table('cache_commerce_shipping_rates', $table);
return t('Created shipping rate cache table for Shipping 2.x.');
}
return t('Skipped creating the shipping rate cache table; it already exists.');
*/
}
/**
* Upgrade from Commerce Shipping 1.x by renaming the commerce_shipping_method
* field on the shipping line item type to commerce_shipping_service and
* converting price components on shipping line items to the generic Shipping
* price component type. Before proceeding, ensure that any shipping method
* modules designed to work with Commerce Shipping 1.x have been disabled but
* not uninstalled. They may be uninstalled after the update is complete.
*/
function commerce_shipping_update_7100(&$sandbox) {
// Prepare the batch process sandbox if it hasn't started yet.
if (empty($sandbox)) {
$sandbox['progress'] = 0;
$sandbox['max'] = db_query("SELECT COUNT(line_item_id) FROM {commerce_line_item} WHERE type = 'shipping'")->fetchColumn();
$sandbox['current'] = 0;
// At this time, also add the commerce_shipping_service field to the
// shipping line item type.
commerce_shipping_line_item_configuration(array('type' => 'shipping'));
}
// Fetch the next 25 shipping line item IDs for upgrade.
$line_item_ids = array_keys(db_query("SELECT line_item_id FROM {commerce_line_item} WHERE type = 'shipping' AND line_item_id > :current ORDER BY line_item_id ASC LIMIT 25", array(':current' => $sandbox['current']))->fetchAllAssoc('line_item_id', PDO::FETCH_ASSOC));
// Loop over the loaded line items to update their unit prices.
foreach (commerce_line_item_load_multiple($line_item_ids) as $line_item) {
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$update_order = FALSE;
// Set the shipping service value to whatever the previous shipping method
// value was if possible.
if (!$line_item_wrapper->commerce_shipping_service->value() && !empty($line_item->commerce_shipping_method)) {
$line_item_wrapper->commerce_shipping_service = $line_item_wrapper->commerce_shipping_method->value();
}
// Extract the total price array from the line item.
$commerce_total = $line_item_wrapper->commerce_total->value();
// Loop over the price components in the unit price.
foreach ($commerce_total['data']['components'] as &$component) {
// If the component came from Commerce Shipping 1.x...
if (substr($component['name'], 0, 5) == 'quote') {
// Store the original price component in the component's data array.
$component['data'] = array(
'original_shipping_component' => array(
'name' => $component['name'],
'data' => $component['data'],
),
);
// Convert the current component type to the generic shipping price
// component type offered by Commerce Shipping 2.x.
$component['name'] = 'shipping';
// Indicate that this order must be saved due to the update.
$update_order = TRUE;
}
}
// Update the unit price to include the updated components and reset the
// quantity of the line item to 1.
$line_item_wrapper->commerce_unit_price = $commerce_total;
$line_item_wrapper->quantity = 1;
$line_item_wrapper->save();
// If the order should be updated, trigger that now.
if ($update_order) {
$order = commerce_order_load($line_item_wrapper->order_id->value());
if ($order) {
commerce_order_save($order);
}
}
$sandbox['progress']++;
$sandbox['current'] = $line_item->line_item_id;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
// If the batch update is now complete...
if ($sandbox['#finished'] == 1) {
// Delete the commerce_shipping_method field from Commerce Shipping 1.x.
field_delete_field('commerce_shipping_method');
return t('Your Commerce Shipping 1.x line items have been updated to work with Commerce Shipping 2.x. However, there is no upgrade path for shipping method modules because of the very different way the two versions of Commerce Shipping integrate with Rules. You must manually reconfigure your shipping methods to work with the 2.x version, in some cases even installing a new module. For example, if you used the Commerce Shipping Flat Rate module with Commerce Shipping 1.x, you must now switch to the Commerce Flat Rate module and reconfigure the conditions that determine when a flat rate shipping service should be presented to customers. For more information, please refer to the project pages for the shipping method modules.');
}
}