Skip to content
This repository was archived by the owner on Apr 27, 2019. It is now read-only.

Commit cd3cea5

Browse files
committed
Fix API for customer. Correct store creation.
1 parent 1f728f2 commit cd3cea5

7 files changed

+94
-73
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ install:
4848
- drupal-ti install
4949
before_script:
5050
- drupal-ti before_script
51-
- drupal-ti --include before.sh
51+
- drupal-ti --include tests/mailchimp-ecommerce-before.sh
5252
script:
5353
- drupal-ti script
5454
after_script:

README.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ For more information about eCommerce 360, please see:
99
http://kb.mailchimp.com/integrations/other-integrations/about-ecommerce360
1010

1111
## Dependencies
12-
* MailChimp: http://drupal.org/project/mailchimp
12+
* MailChimp: https://www.drupal.org/project/mailchimp
1313
* Commerce: https://www.drupal.org/project/commerce
1414

1515
## Installation Notes

includes/mailchimp_ecommerce.admin.inc

+55-32
Original file line numberDiff line numberDiff line change
@@ -9,69 +9,93 @@
99
* The MailChimp eCommerce settings form.
1010
*/
1111
function mailchimp_ecommerce_admin_settings() {
12-
$form['mailchimp_ecommerce_integration'] = array(
13-
'#type' => 'radios',
14-
'#title' => 'Enabled Integration',
15-
'#options' => array(
16-
'' => t('None'),
17-
),
18-
'#default_value' => variable_get('mailchimp_ecommerce_integration', ''),
19-
);
20-
$form['mailchimp_ecommerce_notice'] = array(
12+
$form['mailchimp_ecommerce_notice'] = [
2113
'#markup' => t('This page will allow you to create a store. Once created, you cannot change the list associated with the store.'),
22-
);
23-
$form['mailchimp_ecommerce_store_name'] = array(
14+
];
15+
$form['mailchimp_ecommerce_store_name'] = [
2416
'#type' => 'textfield',
2517
'#title' => t('Store Name'),
2618
'#required' => TRUE,
2719
'#default_value' => variable_get('mailchimp_ecommerce_store_name', ''),
2820
'#description' => t('The name of your store as it should appear in your MailChimp account.'),
29-
);
21+
];
3022

3123
$mailchimp_lists = mailchimp_get_lists();
32-
$list_options = array(
24+
$list_options = [
3325
'' => '-- Select --',
34-
);
26+
];
3527

3628
foreach ($mailchimp_lists as $list_id => $list) {
3729
$list_options[$list_id] = $list->name;
3830
}
3931

4032
if (!empty(variable_get('mailchimp_ecommerce_list_id', ''))) {
4133
$existing_store_id = variable_get('mailchimp_ecommerce_list_id');
42-
$form['mailchimp_ecommerce_list_id_existing'] = array(
34+
$form['mailchimp_ecommerce_list_id_existing'] = [
4335
'#markup' => t('Once created, the list cannot be changed for a given store. This store is connected to the list named') . ' ' . $list_options[$existing_store_id],
44-
);
36+
];
4537
}
4638
else {
47-
$form['mailchimp_ecommerce_list_id'] = array(
39+
$form['mailchimp_ecommerce_list_id'] = [
4840
'#type' => 'select',
4941
'#title' => t('Store List'),
5042
'#required' => TRUE,
5143
'#options' => $list_options,
5244
'#default_value' => variable_get('mailchimp_ecommerce_list_id', ''),
53-
);
45+
];
5446
}
5547

56-
// TODO: Can this be moved into the mailchimp_ecommerce_commerce module?
5748
if (module_exists('commerce')) {
58-
$form['mailchimp_ecommere_commerce_default_currency'] = array(
49+
$form['mailchimp_ecommere_commerce_default_currency'] = [
5950
'#type' => 'checkbox',
6051
'#title' => t('Use Commerce Default Currency'),
6152
'#description' => t('Drupal Commerce was detected as being installed. Check this box to use the default currency of Commerce'),
6253
'#default_value' => variable_get('mailchimp_ecommere_commerce_default_currency', ''),
63-
);
54+
];
6455
}
6556

6657
// @TODO Make this item go away with AJAX if the Drupal commerce box is checked.
67-
$list_options_currency = array(
68-
'' => '-- Select --',
69-
) + mailchimp_ecommerce_get_currency_codes();
70-
$form['mailchimp_ecommerce_currency'] = array(
58+
$list_options_currency = [
59+
'' => '-- Select --',
60+
] + mailchimp_ecommerce_get_currency_codes();
61+
$form['mailchimp_ecommerce_currency'] = [
7162
'#type' => 'select',
7263
'#options' => $list_options_currency,
7364
'#title' => t('Store Currency Code'),
74-
);
65+
'#description' => t('This is overridden if you have selected to use the default currency from Commerce.'),
66+
];
67+
68+
$list_options = [
69+
'' => '-- Select --',
70+
'TRUE' => 'Normal Subscriber',
71+
'FALSE' => 'Transactional Subscriber',
72+
];
73+
74+
$form['mailchimp_ecommerce_opt_in'] = [
75+
'#type' => 'fieldset',
76+
'#title' => t('New User Opt-In Status'),
77+
'#collapsible' => FALSE,
78+
];
79+
$form['mailchimp_ecommerce_opt_in']['mailchimp_ecommerce_opt_in_status_markup'] = [
80+
'#markup' => t('You must decide on the status of customers that entered into
81+
the eCommerce API. By choosing "<b>Normal Subscriber</b>" in the option
82+
below, users will added as normal subscribers. If you choose
83+
"Normal Subscriber", be certain your customers know they are subscribing
84+
to an email list. If you choose "<b>Transactional Subscriber</b>" below,
85+
the users will be added as "transactional" users. Transactional users
86+
cannot be changed via the MailChimp UI. Changing the status of a
87+
"transactional" user call only be accomplished via the API. For additional
88+
information, please read the') . ' ' . l('MailChimp Documentation.', 'http://developer.mailchimp.com/documentation/mailchimp/guides/getting-started-with-ecommerce/#about-subscribers-and-customers', ['external' => TRUE]),
89+
];
90+
91+
$form['mailchimp_ecommerce_opt_in']['mailchimp_ecommerce_opt_in_status'] = [
92+
'#type' => 'select',
93+
'#title' => t('Opt-In Status For Customers'),
94+
'#required' => TRUE,
95+
'#options' => $list_options,
96+
'#default_value' => variable_get('mailchimp_ecommerce_opt_in_status', ''),
97+
'#description' => t('Choose your opt-in status before using this module.'),
98+
];
7599

76100
$settings_form = system_settings_form($form);
77101
$settings_form['#submit'][] = 'mailchimp_ecommerce_admin_settings_submit';
@@ -84,9 +108,11 @@ function mailchimp_ecommerce_admin_settings() {
84108
*/
85109
function mailchimp_ecommerce_admin_settings_submit($form, &$form_state) {
86110
$store_id = variable_get('mailchimp_ecommerce_store_id', NULL);
111+
if (variable_get('mailchimp_ecommerce_store_id', NULL) == NULL) {
112+
$store_id = mailchimp_ecommerce_generate_store_id();
113+
variable_set('mailchimp_ecommerce_store_id', $store_id);
114+
}
87115

88-
// Add / update store only if an eCommerce integration is selected.
89-
if (!empty($form_state['values']['mailchimp_ecommerce_integration'])) {
90116
if ($store_id != NULL) {
91117
// Detect if Commerce is installed and use the default currency.
92118
if (!empty($form_state['values']['mailchimp_ecommere_commerce_default_currency'])) {
@@ -114,8 +140,5 @@ function mailchimp_ecommerce_admin_settings_submit($form, &$form_state) {
114140
$currency);
115141
}
116142
}
117-
}
118-
else {
119-
variable_del('mailchimp_ecommerce_integration');
120-
}
143+
121144
}

mailchimp_ecommerce.install

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
*/
1111
function mailchimp_ecommerce_uninstall() {
1212
variable_del('mailchimp_ecommerce_store_name');
13-
variable_del('mailchimp_ecommerce_integration');
1413
variable_del('mailchimp_ecommerce_list_id');
14+
variable_del('mailchimp_ecommerce_opt_in_status');
1515
}

modules/mailchimp_ecommerce_commerce/mailchimp_ecommerce_commerce.install

+7
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@
1111
function mailchimp_ecommerce_commerce_uninstall() {
1212
variable_del('mailchimp_ecommerce_store_id');
1313
}
14+
15+
/**
16+
* Delete unused variable
17+
*/
18+
function mailchimp_ecommerce_commerce_update_7001() {
19+
variable_del('mailchimp_ecommerce_integration');
20+
}

modules/mailchimp_ecommerce_commerce/mailchimp_ecommerce_commerce.module

+29-38
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,6 @@
55
* Integrates Drupal Commerce with MailChimp eCommerce.
66
*/
77

8-
/**
9-
* Implements hook_form_FORM_ID_alter().
10-
*/
11-
function mailchimp_ecommerce_commerce_form_mailchimp_ecommerce_admin_settings_alter(&$form, &$form_state) {
12-
$form['mailchimp_ecommerce_integration']['#options']['commerce'] = t('Drupal Commerce');
13-
14-
// Add submit handler as first element in submit array.
15-
array_unshift($form['#submit'], 'mailchimp_ecommerce_commerce_integration_admin_settings_submit');
16-
}
17-
18-
/**
19-
* Submit handler for the MailChimp eCommerce settings form.
20-
*/
21-
function mailchimp_ecommerce_commerce_integration_admin_settings_submit($form, &$form_state) {
22-
if (variable_get('mailchimp_ecommerce_store_id', NULL) == NULL) {
23-
$store_id = mailchimp_ecommerce_generate_store_id();
24-
variable_set('mailchimp_ecommerce_store_id', $store_id);
25-
}
26-
}
27-
288
/**
299
* Implements hook_commerce_customer_profile_insert().
3010
*/
@@ -215,21 +195,21 @@ function mailchimp_ecommerce_commerce_batch_add_product($product_id, &$context)
215195
* Implements hook_default_rules_configuration().
216196
*/
217197
function mailchimp_ecommerce_commerce_default_rules_configuration() {
218-
$rules = array();
198+
$rules = [];
219199

220200
// Add a reaction rule to display the default Add to Cart message.
221201
$rule = rules_reaction_rule();
222202

223203
$rule->label = t('Send cart to MailChimp');
224-
$rule->tags = array('Commerce Cart');
204+
$rule->tags = ['Commerce Cart'];
225205
$rule->active = TRUE;
226206

227207
$rule
228208
->event('commerce_cart_product_add')
229209
->event('commerce_cart_product_remove')
230-
->action('mailchimp_ecommerce_commerce_send_cart', array(
210+
->action('mailchimp_ecommerce_commerce_send_cart', [
231211
'commerce_order:select' => 'commerce-order',
232-
));
212+
]);
233213

234214
$rules['mailchimp_ecommerce_commerce_send_cart'] = $rule;
235215

@@ -240,21 +220,21 @@ function mailchimp_ecommerce_commerce_default_rules_configuration() {
240220
* Implements hook_rules_action_info().
241221
*/
242222
function mailchimp_ecommerce_commerce_rules_action_info() {
243-
$actions = array();
223+
$actions = [];
244224

245-
$actions['mailchimp_ecommerce_commerce_send_cart'] = array(
225+
$actions['mailchimp_ecommerce_commerce_send_cart'] = [
246226
'label' => t('Send a cart to MailChimp'),
247-
'parameter' => array(
248-
'commerce_order' => array(
227+
'parameter' => [
228+
'commerce_order' => [
249229
'type' => 'commerce_order',
250230
'label' => t('Order to send'),
251-
),
252-
),
231+
],
232+
],
253233
'group' => t('Commerce Order'),
254-
'callbacks' => array(
234+
'callbacks' => [
255235
'execute' => '_mailchimp_ecommerce_commerce_send_cart',
256-
),
257-
);
236+
],
237+
];
258238

259239
return $actions;
260240
}
@@ -319,19 +299,21 @@ function mailchimp_ecommerce_commerce_parse_customer_profile_address($customer_p
319299
*
320300
* @return object
321301
* Order object in a MailChimp-friendly format.
302+
*
303+
* @throws Exception
304+
* Throws an exception if Opt In Status is not set.
322305
*/
323306
function _mailchimp_ecommerce_commerce_build_order($order_wrapper) {
324307
$user = user_load($order_wrapper->uid->raw());
325-
$currency_code = '';
326-
$order_total = '';
308+
$customer = [];
327309
$billing_address = [];
328310
$lines = [];
329311

330312
if (isset($order_wrapper->commerce_order_total->currency_code)) {
331313
$currency_code = $order_wrapper->commerce_order_total->currency_code->value();
332314
}
333315
if (isset($order_wrapper->commerce_order_total->amount)) {
334-
$order_total = commerce_currency_amount_to_decimal($order_wrapper->commerce_order_total->amount->value(), $currency_code);
316+
$order_total = commerce_currency_amount_to_decimal($order_wrapper->commerce_order_total->amount->value(), $currency_code);
335317
}
336318
if (isset($order_wrapper->commerce_customer_billing->commerce_customer_address)) {
337319
$commerce_billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address->value();
@@ -359,16 +341,25 @@ function _mailchimp_ecommerce_commerce_build_order($order_wrapper) {
359341
'product_id' => $line_item_wrapper->commerce_product->raw(),
360342
'product_variant_id' => $line_item_wrapper->commerce_product->sku->value(),
361343
'quantity' => (int) $line_item_wrapper->quantity->value(),
362-
'price' => commerce_currency_amount_to_decimal($line_item_wrapper->commerce_unit_price->amount->value(), $line_item_wrapper->commerce_unit_price->currency_code->value()),
344+
'price' => commerce_currency_amount_to_decimal($line_item_wrapper->commerce_unit_price->amount->value(), $line_item_wrapper->commerce_unit_price->currency_code->value()),
363345
];
364346
$lines[] = $line;
365347
}
366348
}
367-
349+
// Pull optin status
350+
$optinstatus = variable_get('mailchimp_ecommerce_opt_in_status', NULL);
351+
if (is_null($optinstatus) || empty($optinstatus)) {
352+
watchdog('mailchimp_ecommerce', t('Optin Status has not been set. See admin/config/services/mailchimp/ecommerce.'), [], WATCHDOG_ERROR);
353+
throw new Exception('Optin Status is not set in the MailChimp Ecommerce Module');
354+
}
355+
// Customer parameters used in the 'customer' portions of the API wrapper.
368356
$customer = [
369357
'id' => $user->uid,
358+
'email_address' => $user->mail,
359+
'opt_in_status' => $optinstatus,
370360
];
371361

362+
// Order paramaters that are used as the 'cart' portions of the API wrapper.
372363
$order_data = [
373364
'currency_code' => $currency_code,
374365
'order_total' => $order_total,
File renamed without changes.

0 commit comments

Comments
 (0)