Projet

Général

Profil

Paste
Télécharger (7,92 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / checkout / commerce_checkout.rules_defaults.inc @ b858700c

1
<?php
2

    
3
/**
4
 * @file
5
 * Default rule configurations for Checkout.
6
 */
7

    
8
/**
9
 * Implements hook_default_rules_configuration().
10
 */
11
function commerce_checkout_default_rules_configuration() {
12
  // Store the customer profile entity info for use in default rules.
13
  $customer_profile_entity_info = entity_get_info('commerce_customer_profile');
14

    
15
  $rules = array();
16

    
17
  // Add a reaction rule to set the order created date to the checkout
18
  // completion date. This was added in a point release and thus will be
19
  // disabled by default for updated sites based on a variable set in an update
20
  // hook but enabled by default for new sites.
21
  $rule = rules_reaction_rule();
22

    
23
  $rule->label = t('Set the order created date to the checkout completion date');
24
  $rule->tags = array('Commerce Checkout');
25
  $rule->active = variable_get('enable_commerce_checkout_order_created_date_update', TRUE);
26

    
27
  $rule
28
    ->event('commerce_checkout_complete')
29
    ->action('data_set', array(
30
      'data:select' => 'commerce-order:created',
31
      'value:select' => 'site:current-date',
32
    ));
33

    
34
  $rule->weight = -10;
35

    
36
  $rules['commerce_checkout_order_created_date_update'] = $rule;
37

    
38
  // Add a reaction rule to update an order to the default status of the pending
39
  // order status upon checkout completion.
40
  $rule = rules_reaction_rule();
41

    
42
  $rule->label = t('Update the order status on checkout completion');
43
  $rule->tags = array('Commerce Checkout');
44
  $rule->active = TRUE;
45

    
46
  $rule
47
    ->event('commerce_checkout_complete')
48
    ->action('commerce_order_update_state', array(
49
      'commerce_order:select' => 'commerce-order',
50
      'order_state' => 'pending',
51
    ));
52

    
53
  $rules['commerce_checkout_order_status_update'] = $rule;
54

    
55
  // Add a reaction rule to assign an oder to a pre-existing user account if an
56
  // existing e-mail address is used in checkout.
57
  $rule = rules_reaction_rule();
58

    
59
  $rule->label = t('Assign an anonymous order to a pre-existing user');
60
  $rule->tags = array('Commerce Checkout');
61
  $rule->active = TRUE;
62

    
63
  $rule
64
    ->event('commerce_checkout_complete')
65
    ->condition('data_is', array(
66
      'data:select' => 'commerce-order:uid',
67
      'op' => '==',
68
      'value' => '0',
69
    ))
70
    ->condition('entity_exists', array(
71
      'type' => 'user',
72
      'property' => 'mail',
73
      'value:select' => 'commerce-order:mail',
74
    ))
75
    ->condition('data_is', array(
76
      'data:select' => 'commerce-order:type',
77
      'op' => '==',
78
      'value' => 'commerce_order',
79
    ))
80
    ->action('entity_query', array(
81
      'type' => 'user',
82
      'property' => 'mail',
83
      'value:select' => 'commerce-order:mail',
84
      'limit' => 1,
85
      'entity_fetched:label' => t('Fetched account'),
86
      'entity_fetched:var' => 'account_fetched',
87
    ));
88

    
89
  // Build a loop that updates the order and customer profile uids with the uid
90
  // from the fetched user account.
91
  $loop = rules_loop(array(
92
    'list:select' => 'account-fetched',
93
    'item:var' => 'list_item',
94
    'item:label' => t('Current list item'),
95
    'item:type' => 'user',
96
  ))
97
    ->action('data_set', array(
98
      'data:select' => 'commerce-order:uid',
99
      'value:select' => 'list-item:uid',
100
    ));
101

    
102
  // Accommodate any profile types referenced by the order.
103
  foreach ($customer_profile_entity_info['bundles'] as $type => $data) {
104
    $instance = field_info_instance('commerce_order', 'commerce_customer_' . $type, 'commerce_order');
105

    
106
    if (!empty($instance)) {
107
      $loop
108
        ->action('data_set', array(
109
          'data:select' => 'commerce-order:' . strtr('commerce-customer-' . $type, '_', '-') . ':uid',
110
          'value:select' => 'list-item:uid',
111
        ));
112
    }
113
  }
114

    
115
  // Add the loop to the rule as an action.
116
  $rule->action($loop);
117

    
118
  // Adjust the weight so this rule executes after the order status has been
119
  // updated.
120
  $rule->weight = 1;
121

    
122
  $rules['commerce_checkout_order_convert'] = $rule;
123

    
124
  // Add a reaction rule that creates a new user account during checkout
125
  // completion if the customer specified a non-existent e-mail address. The
126
  // default functionality is to create an active user account with the e-mail
127
  // for administrator created accounts and will always assume the need for
128
  // e-mail verification for setting a password.
129
  $rule = rules_reaction_rule();
130

    
131
  $rule->label = t('Create a new account for an anonymous order');
132
  $rule->tags = array('Commerce Checkout');
133
  $rule->active = TRUE;
134

    
135
  $rule
136
    ->event('commerce_checkout_complete')
137
    ->condition('data_is', array(
138
      'data:select' => 'commerce-order:uid',
139
      'op' => '==',
140
      'value' => '0',
141
    ))
142
    ->condition(rules_condition('entity_exists', array(
143
      'type' => 'user',
144
      'property' => 'mail',
145
      'value:select' => 'commerce-order:mail',
146
    ))->negate())
147
    ->condition('data_is', array(
148
      'data:select' => 'commerce-order:type',
149
      'op' => '==',
150
      'value' => 'commerce_order',
151
    ))
152
    ->action('entity_create', array(
153
      'type' => 'user',
154
      'param_name:select' => 'commerce-order:mail-username',
155
      'param_mail:select' => 'commerce-order:mail',
156
      'entity_created:label' => t('Created account'),
157
      'entity_created:var' => 'account_created',
158
    ))
159
    ->action('data_set', array(
160
      'data:select' => 'account-created:status',
161
      'value' => 1,
162
    ))
163
    ->action('entity_save', array(
164
      'data:select' => 'account-created',
165
      'immediate' => 1,
166
    ))
167
    ->action('entity_query', array(
168
      'type' => 'user',
169
      'property' => 'mail',
170
      'value:select' => 'commerce-order:mail',
171
      'limit' => 1,
172
      'entity_fetched:label' => t('Fetched account'),
173
      'entity_fetched:var' => 'account_fetched',
174
    ));
175

    
176
  // Build a loop that send the account notification e-mail and updates the
177
  // order and customer profile uids with the uid from the fetched user account.
178
  $loop = rules_loop(array(
179
    'list:select' => 'account-fetched',
180
    'item:var' => 'list_item',
181
    'item:label' => t('Current list item'),
182
    'item:type' => 'user',
183
  ))
184
    ->action('send_account_email', array(
185
      'account:select' => 'list-item',
186
      'email_type' => 'register_admin_created',
187
    ))
188
    ->action('data_set', array(
189
      'data:select' => 'commerce-order:uid',
190
      'value:select' => 'list-item:uid',
191
    ));
192

    
193
  // Accommodate any profile types referenced by the order.
194
  foreach ($customer_profile_entity_info['bundles'] as $type => $data) {
195
    $instance = field_info_instance('commerce_order', 'commerce_customer_' . $type, 'commerce_order');
196

    
197
    if (!empty($instance)) {
198
      $loop
199
        ->action('data_set', array(
200
          'data:select' => 'commerce-order:' . strtr('commerce-customer-' . $type, '_', '-') . ':uid',
201
          'value:select' => 'list-item:uid',
202
        ));
203
    }
204
  }
205

    
206
  // Add the loop to the rule as an action.
207
  $rule->action($loop);
208

    
209
  // Adjust the weight so this rule executes after the one checking for a pre-
210
  // existing user account.
211
  $rule->weight = 2;
212

    
213
  $rules['commerce_checkout_new_account'] = $rule;
214

    
215
  // Add a reaction rule to send order e-mail upon checkout completion.
216
  $rule = rules_reaction_rule();
217

    
218
  $rule->label = t('Send an order notification e-mail');
219
  $rule->tags = array('Commerce Checkout');
220
  $rule->active = TRUE;
221

    
222
  $rule
223
    ->event('commerce_checkout_complete')
224
    ->action('mail', array(
225
      'to:select' => 'commerce-order:mail',
226
      'subject' => t('Order [commerce-order:order-number] at [site:name]'),
227
      'message' => t("Thanks for your order [commerce-order:order-number] at [site:name].\n\nIf this is your first order with us, you will receive a separate e-mail with login instructions. You can view your order history with us at any time by logging into our website at:\n\n[site:login-url]\n\nYou can find the status of your current order at:\n\n[commerce-order:customer-url]\n\nPlease contact us if you have any questions about your order."),
228
      'from' => '',
229
    ));
230

    
231
  // Adjust the weight so this rule executes after the order has been updated to
232
  // the proper user account.
233
  $rule->weight = 4;
234

    
235
  $rules['commerce_checkout_order_email'] = $rule;
236

    
237
  return $rules;
238
}