-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtravall-multiple-warehouses-32832.patch
166 lines (158 loc) · 7.13 KB
/
travall-multiple-warehouses-32832.patch
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
diff --git a/commerce_stock_notifications.info b/commerce_stock_notifications.info
index 39ae93f..2fedd60 100644
--- a/commerce_stock_notifications.info
+++ b/commerce_stock_notifications.info
@@ -6,6 +6,6 @@ version = 7.x-1.x
configure = admin/commerce/config/stock/stock-notifications
dependencies[] = commerce
-dependencies[] = commerce_ss
+dependencies[] = commerce_nss
dependencies[] = commerce_stock
dependencies[] = rules
diff --git a/commerce_stock_notifications.install b/commerce_stock_notifications.install
index 86f665d..3f64b8b 100644
--- a/commerce_stock_notifications.install
+++ b/commerce_stock_notifications.install
@@ -51,6 +51,12 @@ function commerce_stock_notifications_schema(){
'length' => 60,
'not null' => TRUE,
),
+ 'stock_field' => array(
+ 'description' => 'Stock field for the relevant warehouse.',
+ 'type' => 'varchar',
+ 'length' => 60,
+ 'not null' => TRUE,
+ ),
'submit_time' => array(
'description' => 'Time the notification request was submitted.',
'type' => 'int',
@@ -68,6 +74,13 @@ function commerce_stock_notifications_schema(){
'size' => 'tiny',
'default' => 0,
),
+ 'language' => array(
+ 'type' => 'varchar',
+ 'length' => 12,
+ 'not null' => TRUE,
+ 'default' => '',
+ 'description' => 'Language in which to send the notification.',
+ ),
),
'indexes' => array(
'email' => array('email'),
@@ -93,3 +106,17 @@ function commerce_stock_notifications_update_7100() {
);
db_add_field( 'commerce_stock_notifications', 'force_send', $spec);
}
+
+/**
+ * Add language field to commerce_stock_notifications table.
+ */
+function commerce_stock_notifications_update_7101() {
+ $spec = array(
+ 'type' => 'varchar',
+ 'length' => 12,
+ 'not null' => TRUE,
+ 'default' => '',
+ 'description' => 'Language in which to send the notification.',
+ );
+ db_add_field( 'commerce_stock_notifications', 'language', $spec);
+}
diff --git a/commerce_stock_notifications.module b/commerce_stock_notifications.module
index 458d6f3..1cfbf83 100644
--- a/commerce_stock_notifications.module
+++ b/commerce_stock_notifications.module
@@ -86,12 +86,15 @@ function commerce_stock_notifications_form_commerce_cart_add_to_cart_form_alter(
// We care about stock if the stock field exists, AND stock is not being
// disabled by the override option.
- $stock_managed = (commerce_ss_product_type_enabled($product->type) && commerce_ss_product_not_disabled_by_override($product));
+ $delivery_country = travall_delivery_country_get_country();
+ $warehouse = travall_delivery_country_get_warehouse($delivery_country);
+ $stock_managed = (commerce_nss_product_type_enabled($product->type, $warehouse) && commerce_nss_product_not_disabled_by_override($product));
// Adds the subscribing form under at the bottom of the cart form in order to
// let the user register to be notified.
- if ($stock_managed && $product_wrapper->commerce_stock->value() <= '0.00') {
- $form['commerce_stock_notifications'] = commerce_stock_notifications_customer_form($form_state, $form_state['line_item']->data['context']['display_path']);
+ $stock_field = 'commerce_stock_' . strtolower($warehouse);
+ if ($stock_managed && $product_wrapper->{$stock_field}->value() <= '0.00') {
+ $form['commerce_stock_notifications'] = commerce_stock_notifications_customer_form($form_state, $form_state['line_item']->data['context']['display_path'], $stock_field);
$form['commerce_stock_notifications']['#weight'] = 100;
$form['#validate'] = array();
$form['#submit'] = array('commerce_stock_notifications_customer_form_submit');
@@ -102,11 +105,12 @@ function commerce_stock_notifications_form_commerce_cart_add_to_cart_form_alter(
* Custom form to let customers register their email address to be notified
* when product is back in stock.
*/
-function commerce_stock_notifications_customer_form(&$form_state, $context = NULL) {
+function commerce_stock_notifications_customer_form(&$form_state, $context = NULL, $stock_field) {
global $user;
$config = variable_get('commerce_stock_notifications', commerce_stock_notification_default_settings());
$form_state['notify']['config'] = $config;
$form_state['notify']['context'] = $context;
+ $form_state['notify']['stock_field'] = $stock_field;
$form['commerce_stock_notifications'] = array(
'#type' => 'fieldset',
@@ -140,6 +144,7 @@ function commerce_stock_notifications_customer_form_submit($form, $form_state) {
$notify = $form_state['notify'];
$config = $notify['config'];
$context = $notify['context'];
+ $stock_field = $notify['stock_field'];
$product_id = $form_state['input']['product_id'];
$email = $form_state['values']['notify_email'];
@@ -149,11 +154,16 @@ function commerce_stock_notifications_customer_form_submit($form, $form_state) {
drupal_set_message(t('@duplicate_message', array('@duplicate_message' => $config['duplicate_message'])));
}
else {
+ global $language;
+ $lang = $language->language;
+
$record = array (
+ 'stock_field' => $stock_field,
'product_id' => $product_id,
'email' => $email,
'submit_time' => REQUEST_TIME,
'context' => $context,
+ 'language' => $lang,
);
drupal_write_record('commerce_stock_notifications', $record);
drupal_set_message(t('@success_message', array('@success_message' => $config['success_message'])));
@@ -211,9 +221,13 @@ function commerce_stock_notifications_cron() {
$queue = DrupalQueue::get('commerce_stock_notifications');
if ($queue->numberOfItems() == 0) {
- $query = db_select('commerce_stock_notifications', 'csn');
- $query->join('field_data_commerce_stock', 'fdcs', 'csn.product_id = fdcs.entity_id');
- $query->fields('csn', array(
+ // Go over each warehouse separately.
+ $warehouses = travall_delivery_country_list_warehouses();
+ foreach ($warehouses as $warehouse) {
+ $warehouse = strtolower($warehouse);
+ $query = db_select('commerce_stock_notifications', 'csn');
+ $query->join("field_data_commerce_stock_$warehouse", 'fdcs', 'csn.product_id = fdcs.entity_id');
+ $query->fields('csn', array(
'email',
'product_id',
'notification_id',
@@ -221,15 +235,18 @@ function commerce_stock_notifications_cron() {
'context',
'force_send',
))
- ->isNull('csn.sent_time')
- ->condition(db_or()->condition('fdcs.commerce_stock_value', 0, '>')->condition('csn.force_send', 1))
- ->orderBy('submit_time', 'ASC')
- ->range(0, 500);
+ ->condition('csn.stock_field', 'commerce_stock_' . $warehouse)
+ ->isNull('csn.sent_time')
+ ->condition(db_or()->condition('fdcs.commerce_stock_' . $warehouse . '_value', 0, '>')->condition('csn.force_send', 1))
+ ->orderBy('submit_time', 'ASC')
+ ->range(0, 500);
- $result = $query->execute();
- foreach ($result as $record) {
- $queue->createItem($record);
+ $result = $query->execute();
+ foreach ($result as $record) {
+ $queue->createItem($record);
+ }
}
+
}
}