-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathozmid-woocommerce-integration.php
140 lines (115 loc) · 5.53 KB
/
ozmid-woocommerce-integration.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
// Name: OZMID Complete WooCommerce Integration
// OZMID Complete WooCommerce Integration - WooCommerce 8.0.2
// Add new field to product category
add_action('product_cat_add_form_fields', 'woocommerce_add_category_id_field');
add_action('product_cat_edit_form_fields', 'woocommerce_edit_category_id_field', 10, 2);
function woocommerce_add_category_id_field() {
?>
<div class="form-field">
<label for="OZMID"><?php _e('OZMID', 'woocommerce'); ?></label>
<input type="text" id="OZMID" name="OZMID" />
<p class="description"><?php _e('Enter a unique OZMID for this category.', 'woocommerce'); ?></p>
</div>
<div class="form-field">
<label for="generate_ozmx"><?php _e('OZMX-Token generieren?', 'woocommerce'); ?></label>
<input type="checkbox" id="generate_ozmx" name="generate_ozmx" value="1" />
<p class="description"><?php _e('Check if OZMX tokens should be generated for this category.', 'woocommerce'); ?></p>
</div>
<?php
}
function woocommerce_edit_category_id_field($term, $taxonomy) {
$OZMID = get_term_meta($term->term_id, 'OZMID', true);
$generate_ozmx = get_term_meta($term->term_id, 'generate_ozmx', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="OZMID"><?php _e('OZMID', 'woocommerce'); ?></label></th>
<td>
<input type="text" id="OZMID" name="OZMID" value="<?php echo esc_attr($OZMID) ? esc_attr($OZMID) : ''; ?>" />
<p class="description"><?php _e('Enter a unique OZMID for this category.', 'woocommerce'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="generate_ozmx"><?php _e('OZMX-Token generieren?', 'woocommerce'); ?></label></th>
<td>
<input type="checkbox" id="generate_ozmx" name="generate_ozmx" value="1" <?php checked($generate_ozmx, 1); ?> />
<p class="description"><?php _e('Check if OZMX tokens should be generated for this category.', 'woocommerce'); ?></p>
</td>
</tr>
<?php
}
add_action('created_product_cat', 'woocommerce_save_category_id_field', 10, 2);
add_action('edited_product_cat', 'woocommerce_save_category_id_field', 10, 2);
function woocommerce_save_category_id_field($term_id, $tt_id) {
if (isset($_POST['OZMID']) && '' !== $_POST['OZMID']) {
$OZMID = sanitize_text_field($_POST['OZMID']);
update_term_meta($term_id, 'OZMID', $OZMID);
}
if (isset($_POST['generate_ozmx'])) {
update_term_meta($term_id, 'generate_ozmx', 1);
} else {
update_term_meta($term_id, 'generate_ozmx', 0);
}
}
// Display OZMID, year and product ID on product page
add_action('woocommerce_single_product_summary', 'display_product_info', 15);
function display_product_info() {
global $product;
$product_id = $product->get_id();
$product_year = get_the_date('Y', $product_id);
$product_categories = get_the_terms($product_id, 'product_cat');
if ($product_categories && !is_wp_error($product_categories)) {
$single_cat = array_shift($product_categories);
$OZMID = get_term_meta($single_cat->term_id, 'OZMID', true);
echo '<p>' . __('OZMID: ', 'woocommerce') . $OZMID . '</p>';
echo '<p>' . __('Year: ', 'woocommerce') . $product_year . '</p>';
echo '<p>' . __('Product ID: ', 'woocommerce') . $product_id . '</p>';
}
}
// OZM ID shortcode
function woocommerce_product_info_shortcode() {
global $product;
if ($product) {
$product_id = $product->get_id();
$product_year = get_the_date('Y', $product_id);
$product_categories = get_the_terms($product_id, 'product_cat');
if ($product_categories && !is_wp_error($product_categories)) {
$single_cat = array_shift($product_categories);
$OZMID = get_term_meta($single_cat->term_id, 'OZMID', true);
return '' . $OZMID . '' . $product_year . '' . $product_id;
}
}
return '';
}
add_shortcode('product_info', 'woocommerce_product_info_shortcode');
// OZM ID and product info to checkout
add_filter('woocommerce_get_item_data', 'display_product_info_in_cart', 10, 2);
function display_product_info_in_cart($item_data, $cart_item) {
$product_id = $cart_item['product_id'];
$product_categories = get_the_terms($product_id, 'product_cat');
if ($product_categories && !is_wp_error($product_categories)) {
$single_cat = array_shift($product_categories);
$OZMID = get_term_meta($single_cat->term_id, 'OZMID', true);
$product_year = get_the_date('Y', $product_id);
$product_info = '' . $OZMID . '' . $product_year . '' . $product_id;
$item_data[] = array(
'key' => __('ID', 'woocommerce'),
'value' => wc_clean($product_info),
'display' => '',
);
}
return $item_data;
}
// Add product info to order meta
add_action('woocommerce_checkout_create_order_line_item', 'add_product_info_to_order_items', 10, 4);
function add_product_info_to_order_items($item, $cart_item_key, $values, $order) {
$product_id = $values['product_id'];
$product_categories = get_the_terms($product_id, 'product_cat');
if ($product_categories && !is_wp_error($product_categories)) {
$single_cat = array_shift($product_categories);
$OZMID = get_term_meta($single_cat->term_id, 'OZMID', true);
$product_year = get_the_date('Y', $product_id);
$product_info = '' . $OZMID . '' . $product_year . '' . $product_id;
$item->add_meta_data(__('ID', 'woocommerce'), $product_info, true);
}
}