Import users from CSV with meta - Version 1.15.3.3

Version Description

  • WooCommerce Membership addon improved, now you can only assign users to a plan using the membership_plan_id
Download this release

Release Info

Developer carazo
Plugin Icon 128x128 Import users from CSV with meta
Version 1.15.3.3
Comparing to
See all releases

Code changes from version 1.15.3.2 to 1.15.3.3

addons/woocommerce-membership.php CHANGED
@@ -18,83 +18,105 @@ function acui_wm_documentation_after_plugins_activated(){
18
  ?>
19
  <tr valign="top">
20
  <th scope="row"><?php _e( "WooCommerce Membership is activated", 'import-users-from-csv-with-meta' ); ?></th>
21
- <td><?php _e( "You can use the <strong>columns in the CSV format created by WooCommercer Membership</strong> in order to import data from this plugin.", 'import-users-from-csv-with-meta' ); ?>. <a href="https://docs.woocommerce.com/document/woocommerce-memberships-import-and-export/"><?php _e( "Read more about columns and formats", 'import-users-from-csv-with-meta' ); ?></a>
 
 
 
 
22
  </td>
23
  </tr>
24
  <?php
25
  }
26
 
27
  function acui_wm_post_import_single_user( $headers, $row, $user_id ){
28
- $keys = array( 'member_first_name', 'member_last_name', 'member_email', 'membership_plan_id', 'membership_plan_slug', 'membership_plan', 'membership_status', 'member_since', 'membership_expiration' );
29
- $columns = array();
30
 
31
- foreach ( $keys as $key ) {
32
- $columns[ $key ] = array_search ( $key, $headers );
33
- }
34
 
35
- $membership_plan_id = isset( $columns['membership_plan_id'] ) && ! empty( $row[ $columns['membership_plan_id'] ] ) ? (int) $row[ $columns['membership_plan_id'] ] : null;
36
- $membership_plan_slug = isset( $columns['membership_plan_slug'] ) && ! empty( $row[ $columns['membership_plan_slug'] ] ) ? $row[ $columns['membership_plan_slug'] ] : null;
37
- $membership_plan = null;
38
 
39
- if ( is_int( $membership_plan_id ) ) {
40
- $membership_plan = wc_memberships_get_membership_plan( $membership_plan_id );
41
- }
42
 
43
- if ( ! $membership_plan && ! empty( $membership_plan_slug ) ) {
44
- $membership_plan = wc_memberships_get_membership_plan( $membership_plan_slug );
45
- }
46
 
47
- // try to get an existing user membership from an id
48
- $user_membership_id = isset( $columns['user_membership_id'] ) && ! empty( $row[ $columns['user_membership_id'] ] ) ? (int) $row[ $columns['user_membership_id'] ] : null;
49
- $existing_user_membership = is_int( $user_membership_id ) ? wc_memberships_get_user_membership( $user_membership_id ) : null;
50
 
51
- if ( ! $membership_plan && ! $existing_user_membership ) {
52
- return;
53
- } elseif ( ! $existing_user_membership && false ) {
54
- return;
55
- }
56
 
57
- $import_data = array();
58
-
59
- $import_data['membership_plan_id'] = $membership_plan_id;
60
- $import_data['membership_plan_slug'] = $membership_plan_slug;
61
- $import_data['membership_plan_name'] = isset( $columns['membership_plan'] ) && ! empty( $row[ $columns['membership_plan'] ] ) ? $row[ $columns['membership_plan'] ] : null;
62
- $import_data['membership_plan'] = $membership_plan;
63
- $import_data['user_membership_id'] = $user_membership_id;
64
- $import_data['user_membership'] = $existing_user_membership;
65
- $import_data['user_id'] = $user_id;
66
- $import_data['user_name'] = isset( $columns['user_name'] ) && ! empty( $row[ $columns['user_name'] ] ) ? $row[ $columns['user_name'] ] : null;
67
- $import_data['product_id'] = isset( $columns['product_id'] ) && ! empty( $row[ $columns['product_id'] ] ) ? $row[ $columns['product_id'] ] : null;
68
- $import_data['order_id'] = isset( $columns['order_id'] ) && ! empty( $row[ $columns['order_id'] ] ) ? $row[ $columns['order_id'] ] : null;
69
- $import_data['member_email'] = isset( $columns['member_email'] ) && ! empty( $row[ $columns['member_email'] ] ) ? $row[ $columns['member_email'] ] : null;
70
- $import_data['member_first_name'] = isset( $columns['member_first_name'] ) && ! empty( $row[ $columns['member_first_name'] ] ) ? $row[ $columns['member_first_name'] ] : null;
71
- $import_data['member_last_name'] = isset( $columns['member_last_name'] ) && ! empty( $row[ $columns['member_last_name'] ] ) ? $row[ $columns['member_last_name'] ] : null;
72
- $import_data['membership_status'] = isset( $columns['membership_status'] ) && ! empty( $row[ $columns['membership_status'] ] ) ? $row[ $columns['membership_status'] ] : null;
73
- $import_data['member_since'] = isset( $columns['member_since'] ) && ! empty( $row[ $columns['member_since'] ] ) ? $row[ $columns['member_since'] ] : null;
74
- $import_data['membership_expiration'] = isset( $columns['membership_expiration'] ) && isset( $row[ $columns['membership_expiration'] ] ) ? $row[ $columns['membership_expiration'] ] : null;
75
-
76
- $action = 'create';
77
- $import_data = (array) apply_filters( 'wc_memberships_csv_import_user_memberships_data', $import_data, $action, $columns, $row );
78
-
79
- $user_membership = null;
80
-
81
- if ( isset( $import_data['membership_plan'] ) && $import_data['membership_plan'] instanceof WC_Memberships_Membership_Plan ) {
82
- if ( wc_memberships_is_user_member( $user_id, $import_data['membership_plan'] ) ) {
83
- return false;
84
  }
85
 
86
- $user_membership = wc_memberships_create_user_membership( array(
87
- 'user_membership_id' => 0,
88
- 'plan_id' => $import_data['membership_plan']->get_id(),
89
- 'user_id' => $user_id,
90
- 'product_id' => ! empty( $import_data['product_id'] ) ? (int) $import_data['product_id'] : 0,
91
- 'order_id' => ! empty( $import_data['order_id'] ) ? (int) $import_data['order_id'] : 0,
92
- ), 'create' );
93
- }
 
94
 
95
- acui_vm_update_user_membership_meta( $user_membership, $action, $import_data );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
- do_action( 'wc_memberships_csv_import_user_membership', $user_membership, $action, $import_data );
 
 
 
 
 
 
 
 
 
98
  }
99
 
100
  function acui_vm_update_user_membership_meta( WC_Memberships_User_Membership $user_membership, $action, array $data ) {
18
  ?>
19
  <tr valign="top">
20
  <th scope="row"><?php _e( "WooCommerce Membership is activated", 'import-users-from-csv-with-meta' ); ?></th>
21
+ <td>
22
+ <ol>
23
+ <li><strong><?php _e( "Import all membership information", 'import-users-from-csv-with-meta' ); ?></strong>: <?php _e( "You can use the <strong>columns in the CSV format created by WooCommercer Membership</strong> in order to import data from this plugin.", 'import-users-from-csv-with-meta' ); ?>. <a href="https://docs.woocommerce.com/document/woocommerce-memberships-import-and-export/"><?php _e( "Read more about columns and formats", 'import-users-from-csv-with-meta' ); ?></a>.</li>
24
+ <li><strong><?php _e( "Add users to membership plans", 'import-users-from-csv-with-meta' ); ?></strong>: <?php _e( "In this case you will only have to use <strong>membership_plan_id</strong> column in order to associate a user to their membership plan", 'import-users-from-csv-with-meta' ); ?>.</li>
25
+ </ol>
26
  </td>
27
  </tr>
28
  <?php
29
  }
30
 
31
  function acui_wm_post_import_single_user( $headers, $row, $user_id ){
32
+ $pos = array_search( 'membership_plan_id', $headers );
 
33
 
34
+ if( $pos === FALSE )
35
+ return;
 
36
 
37
+ $pos_member_last_name = array_search( 'member_last_name', $headers ); // we search if there is only membership_plan_id or if there are more data
38
+ $full_membership_import = ( $pos_member_last_name !== FALSE );
 
39
 
40
+ if( $full_membership_import ){
41
+ $keys = array( 'member_first_name', 'member_last_name', 'member_email', 'membership_plan_id', 'membership_plan_slug', 'membership_plan', 'membership_status', 'member_since', 'membership_expiration' );
42
+ $columns = array();
43
 
44
+ foreach ( $keys as $key ) {
45
+ $columns[ $key ] = array_search ( $key, $headers );
46
+ }
47
 
48
+ $membership_plan_id = isset( $columns['membership_plan_id'] ) && ! empty( $row[ $columns['membership_plan_id'] ] ) ? (int) $row[ $columns['membership_plan_id'] ] : null;
49
+ $membership_plan_slug = isset( $columns['membership_plan_slug'] ) && ! empty( $row[ $columns['membership_plan_slug'] ] ) ? $row[ $columns['membership_plan_slug'] ] : null;
50
+ $membership_plan = null;
51
 
52
+ if ( is_int( $membership_plan_id ) ) {
53
+ $membership_plan = wc_memberships_get_membership_plan( $membership_plan_id );
54
+ }
 
 
55
 
56
+ if ( ! $membership_plan && ! empty( $membership_plan_slug ) ) {
57
+ $membership_plan = wc_memberships_get_membership_plan( $membership_plan_slug );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  }
59
 
60
+ // try to get an existing user membership from an id
61
+ $user_membership_id = isset( $columns['user_membership_id'] ) && ! empty( $row[ $columns['user_membership_id'] ] ) ? (int) $row[ $columns['user_membership_id'] ] : null;
62
+ $existing_user_membership = is_int( $user_membership_id ) ? wc_memberships_get_user_membership( $user_membership_id ) : null;
63
+
64
+ if ( ! $membership_plan && ! $existing_user_membership ) {
65
+ return;
66
+ } elseif ( ! $existing_user_membership && false ) {
67
+ return;
68
+ }
69
 
70
+ $import_data = array();
71
+
72
+ $import_data['membership_plan_id'] = $membership_plan_id;
73
+ $import_data['membership_plan_slug'] = $membership_plan_slug;
74
+ $import_data['membership_plan_name'] = isset( $columns['membership_plan'] ) && ! empty( $row[ $columns['membership_plan'] ] ) ? $row[ $columns['membership_plan'] ] : null;
75
+ $import_data['membership_plan'] = $membership_plan;
76
+ $import_data['user_membership_id'] = $user_membership_id;
77
+ $import_data['user_membership'] = $existing_user_membership;
78
+ $import_data['user_id'] = $user_id;
79
+ $import_data['user_name'] = isset( $columns['user_name'] ) && ! empty( $row[ $columns['user_name'] ] ) ? $row[ $columns['user_name'] ] : null;
80
+ $import_data['product_id'] = isset( $columns['product_id'] ) && ! empty( $row[ $columns['product_id'] ] ) ? $row[ $columns['product_id'] ] : null;
81
+ $import_data['order_id'] = isset( $columns['order_id'] ) && ! empty( $row[ $columns['order_id'] ] ) ? $row[ $columns['order_id'] ] : null;
82
+ $import_data['member_email'] = isset( $columns['member_email'] ) && ! empty( $row[ $columns['member_email'] ] ) ? $row[ $columns['member_email'] ] : null;
83
+ $import_data['member_first_name'] = isset( $columns['member_first_name'] ) && ! empty( $row[ $columns['member_first_name'] ] ) ? $row[ $columns['member_first_name'] ] : null;
84
+ $import_data['member_last_name'] = isset( $columns['member_last_name'] ) && ! empty( $row[ $columns['member_last_name'] ] ) ? $row[ $columns['member_last_name'] ] : null;
85
+ $import_data['membership_status'] = isset( $columns['membership_status'] ) && ! empty( $row[ $columns['membership_status'] ] ) ? $row[ $columns['membership_status'] ] : null;
86
+ $import_data['member_since'] = isset( $columns['member_since'] ) && ! empty( $row[ $columns['member_since'] ] ) ? $row[ $columns['member_since'] ] : null;
87
+ $import_data['membership_expiration'] = isset( $columns['membership_expiration'] ) && isset( $row[ $columns['membership_expiration'] ] ) ? $row[ $columns['membership_expiration'] ] : null;
88
+
89
+ $action = 'create';
90
+ $import_data = (array) apply_filters( 'wc_memberships_csv_import_user_memberships_data', $import_data, $action, $columns, $row );
91
+
92
+ $user_membership = null;
93
+
94
+ if ( isset( $import_data['membership_plan'] ) && $import_data['membership_plan'] instanceof WC_Memberships_Membership_Plan ) {
95
+ if ( wc_memberships_is_user_member( $user_id, $import_data['membership_plan'] ) ) {
96
+ return false;
97
+ }
98
+
99
+ $user_membership = wc_memberships_create_user_membership( array(
100
+ 'user_membership_id' => 0,
101
+ 'plan_id' => $import_data['membership_plan']->get_id(),
102
+ 'user_id' => $user_id,
103
+ 'product_id' => ! empty( $import_data['product_id'] ) ? (int) $import_data['product_id'] : 0,
104
+ 'order_id' => ! empty( $import_data['order_id'] ) ? (int) $import_data['order_id'] : 0,
105
+ ), 'create' );
106
+ }
107
+
108
+ acui_vm_update_user_membership_meta( $user_membership, $action, $import_data );
109
 
110
+ do_action( 'wc_memberships_csv_import_user_membership', $user_membership, $action, $import_data );
111
+ }
112
+ else{
113
+ $membership_plan_id = absint( $row[ $pos ] );
114
+ $args = array(
115
+ 'plan_id' => $membership_plan_id,
116
+ 'user_id' => $user_id,
117
+ );
118
+ wc_memberships_create_user_membership( $args );
119
+ }
120
  }
121
 
122
  function acui_vm_update_user_membership_meta( WC_Memberships_User_Membership $user_membership, $action, array $data ) {
import-users-from-csv-with-meta.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Import and export users and customers
4
  Plugin URI: https://www.codection.com
5
  Description: Using this plugin you will be able to import and export users or customers choosing many options and interacting with lots of other plugins
6
- Version: 1.15.3.2
7
  Author: codection
8
  Author URI: https://codection.com
9
  License: GPL2
@@ -11,7 +11,6 @@ License URI: https://www.gnu.org/licenses/gpl-2.0.html
11
  Text Domain: import-users-from-csv-with-meta
12
  Domain Path: /languages
13
  */
14
-
15
  if ( ! defined( 'ABSPATH' ) )
16
  exit;
17
 
3
  Plugin Name: Import and export users and customers
4
  Plugin URI: https://www.codection.com
5
  Description: Using this plugin you will be able to import and export users or customers choosing many options and interacting with lots of other plugins
6
+ Version: 1.15.3.3
7
  Author: codection
8
  Author URI: https://codection.com
9
  License: GPL2
11
  Text Domain: import-users-from-csv-with-meta
12
  Domain Path: /languages
13
  */
 
14
  if ( ! defined( 'ABSPATH' ) )
15
  exit;
16
 
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://codection.com/go/donate-import-users-from-csv-with-meta/
4
  Tags: csv, import, importer, meta data, meta, user, users, user meta, editor, profile, custom, fields, delimiter, update, insert
5
  Requires at least: 3.4
6
  Tested up to: 5.3.2
7
- Stable tag: 1.15.3.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -98,6 +98,9 @@ Plugin will automatically detect:
98
 
99
  == Changelog ==
100
 
 
 
 
101
  = 1.15.3.2 =
102
  * Bug fixed
103
 
4
  Tags: csv, import, importer, meta data, meta, user, users, user meta, editor, profile, custom, fields, delimiter, update, insert
5
  Requires at least: 3.4
6
  Tested up to: 5.3.2
7
+ Stable tag: 1.15.3.3
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
98
 
99
  == Changelog ==
100
 
101
+ = 1.15.3.3 =
102
+ * WooCommerce Membership addon improved, now you can only assign users to a plan using the membership_plan_id
103
+
104
  = 1.15.3.2 =
105
  * Bug fixed
106