Skip to content

Commit 6717e88

Browse files
committed
2 parents 973593e + d5dbc4d commit 6717e88

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

wc-api-custom-meta.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ public static function initialize()
8888
2
8989
);
9090
}
91+
// Add a hook to update product variations
92+
add_action(
93+
'woocommerce_api_save_product_variation',
94+
array('Academe_Wc_Api_Custom_Meta', 'updateVariationCustomMeta'),
95+
10,
96+
3
97+
);
9198
}
9299

93100
/**
@@ -115,6 +122,27 @@ public static function fetchCustomMeta($product_data, $product, $fields, $server
115122
$meta = $all_meta;
116123

117124
$product_data['meta'] = $meta;
125+
126+
if(isset($product_data['variations'])) {
127+
foreach($product_data['variations'] as $k => &$variation) {
128+
$variation_id = $variation['id'];
129+
130+
$all_meta = get_post_meta($variation_id);
131+
132+
// Filter out meta we don't want.
133+
$all_meta = array_diff_key($all_meta, array_flip(static::$protected_fields));
134+
135+
// Unserialize the meta field data where necessary.
136+
foreach($all_meta as $key => &$value) {
137+
$value = maybe_unserialize(reset($value));
138+
}
139+
unset($value);
140+
141+
$meta = $all_meta;
142+
143+
$variation['meta'] = $meta;
144+
}
145+
}
118146
}
119147

120148
return $product_data;
@@ -158,6 +186,13 @@ public static function updateCustomMeta($id, $data) {
158186
}
159187
}
160188
}
189+
190+
/**
191+
* Update or create a product variation using above function.
192+
*/
193+
public static function updateVariationCustomMeta($id, $menu_order, $data) {
194+
Academe_Wc_Api_Custom_Meta::updateCustomMeta($id, $data);
195+
}
161196
}
162197

163198
Academe_Wc_Api_Custom_Meta::initialize();

0 commit comments

Comments
 (0)