woocommerce-custom-fields.php
2.39 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
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if( !is_plugin_active( 'woocommerce-custom-fields/woocommerce-custom-fields.php' ) ){
return;
}
class ACUI_WCF{
function __construct(){
add_filter( 'acui_restricted_fields', array( $this, 'restricted_fields' ), 10, 1 );
add_filter( 'acui_not_meta_fields', array( $this, 'restricted_fields' ), 10, 1 );
add_action( 'acui_documentation_after_plugins_activated', array( $this, 'documentation' ) );
add_action( 'post_acui_import_single_user', array( $this, 'import' ), 10, 3 );
}
function get_fields(){
$customer_fields = get_posts( array(
'post_type' => 'wccf_user_field',
'posts_per_page' => -1,
) );
$result = array();
foreach ( $customer_fields as $custom_field ) {
$result[ $custom_field->ID ] = get_post_meta( $custom_field->ID, 'key', true );
}
return $result;
}
function documentation(){
?>
<tr valign="top">
<th scope="row"><?php _e( "WooCommerce Custom Fields is activated", 'import-users-from-csv-with-meta' ); ?></th>
<td>
<?php _e( "You can import those fields, look at fields you can import using the column names shown below.", 'import-users-from-csv-with-meta' ); ?>.
<ul style="list-style:square inside none; margin-left:2em;">
<?php foreach ( $this->get_fields() as $field => $field_id): ?>
<li><?php echo $field; ?></li>
<?php endforeach; ?>
</ul>
</td>
</tr>
<?php
}
function restricted_fields( $acui_restricted_fields ){
return array_merge( $acui_restricted_fields, array_keys( $this->get_fields() ) );
}
function import( $headers, $row, $user_id ){
$context = 'user_field';
$columns = array();
$data = array();
foreach ( $this->get_fields() as $key => $value ) {
$pos = array_search( $value, $headers );
if( $pos !== FALSE ){
$columns[ $value ] = $pos;
$data[ $value ] = $row[ $columns[ $value ] ];
}
}
$values = array();
foreach ( $this->get_fields() as $key => $value ) {
$values[ $key ] = array( 'value' => $data[ $value ], 'data' => array(), 'files' => array() );
}
$item = RightPress_Help::wc_get_customer( $user_id );
if( empty( $values ) || !is_array( $values ) ){
return;
}
foreach( $values as $field_id => $field_value ) {
$field = WCCF_Field_Controller::get( $field_id, 'wccf_' . $context );
if ( !$field ) {
continue;
}
$field->store_value( $item, $field_value );
}
$item->save();
}
}
new ACUI_WCF();