Skip to content

Commit 406332f

Browse files
author
Consilience
committed
first commit
0 parents  commit 406332f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

wc-api-custom-meta.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/*
3+
Plugin Name: WC API Custom Metafields
4+
Plugin URI: https://github.com/academe/TBC
5+
Description: Allows custom meta fields to be added to products when creating or updating.
6+
Version: 0.5
7+
Author: Jason Judge
8+
Author URI: http://academe.co.uk
9+
*/
10+
11+
// Want to hook into woocommerce_api_process_product_meta_{product_type} for all product types.
12+
13+
add_action('woocommerce_api_process_product_meta_simple', 'academe_wc_api_custom_meta', 10, 2);
14+
add_action('woocommerce_api_process_product_meta_variable', 'academe_wc_api_custom_meta', 10, 2);
15+
add_action('woocommerce_api_process_product_grouped', 'academe_wc_api_custom_meta', 10, 2);
16+
add_action('woocommerce_api_process_product_external', 'academe_wc_api_custom_meta', 10, 2);
17+
18+
function academe_wc_api_custom_meta($id, $data) {
19+
if (!empty($data['custom_meta']) && is_array($data['custom_meta'])) {
20+
foreach($data['custom_meta'] as $field_name => $field_value) {
21+
update_post_meta($id, $field_name, wc_clean($field_value));
22+
}
23+
}
24+
25+
if (!empty($data['custom_meta']) && is_array($data['remove_custom_meta'])) {
26+
foreach($data['remove_custom_meta'] as $key => $value) {
27+
// If the key is numeric, then assume $value is the field name
28+
// and all entries need to be deleted. Otherwise is is a specfic value
29+
// of a named meta field that should be removed.
30+
31+
if (is_numeric($key)) {
32+
delete_post_meta($id, $value);
33+
} else {
34+
delete_post_meta($id, $key, $value);
35+
}
36+
}
37+
}
38+
}
39+

0 commit comments

Comments
 (0)