This repository has been archived by the owner on Jul 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom_wooCommerce_field.php
100 lines (88 loc) · 2.48 KB
/
custom_wooCommerce_field.php
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
<?php
/**
* Class Name: Woo_Custom_Meta_Fields
* GitHub URI: https://github.com/mattiasghodsian/wooCustomMetaFields
* Description: This class Simplifys adding fields to woocommer product data meta
* Version: 1.0.0
* Author: Mattias Ghodsian
* Author URI: http://www.nexxoz.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Donate a cup of coffee: https://www.paypal.me/MattiasG
*/
class Woo_Custom_Meta_Fields {
private $fields = [];
/**
* Execute
*/
public function init($array)
{
if (!is_array($array) && !empty($array))
return false;
add_action(
$array['tab'],
[$this, 'fields']
);
add_action('woocommerce_process_product_meta',
[$this, 'save']
);
}
/**
* add field to class
*
* @param $fields array
* return string
*/
public function addFields($fields)
{
if (is_array($fields) && !empty($fields)) {
$this->fields = $fields;
}else{
return 'variable must be array';
}
}
/**
* save all fields
* return null
*/
public function save()
{
foreach ($this->fields as $key => $field) {
$postField = $_POST[ $field['attr']['id'] ];
if (!empty($postField))
update_post_meta($post_id, $field['attr']['id'], esc_attr($postField));
}
}
/**
* Print out the fields
* return null
*/
public function fields()
{
global $woocommerce, $post;
echo '<div class="woo_custom_meta_field">';
foreach ($this->fields as $key => $field) {
switch ($field['type']) {
case 'text':
woocommerce_wp_text_input($field['attr']);
break;
case 'textarea':
woocommerce_wp_textarea_input($field['attr']);
break;
case 'select':
woocommerce_wp_select($field['attr']);
break;
case 'radio':
woocommerce_wp_radio($field['attr']);
break;
case 'checkbox':
woocommerce_wp_checkbox($field['attr']);
break;
case 'hidden':
woocommerce_wp_hidden_input($field['attr']);
break;
}
}
echo '</div>';
}
}