@@ -327,6 +327,62 @@ public function prepare_line_items( $posted, $action = 'create', $item = null )
327
327
return $ item ;
328
328
}
329
329
330
+ /**
331
+ * Maybe set item meta if posted.
332
+ *
333
+ * @param WC_Order_Item $item Order item data.
334
+ * @param array $posted Request data.
335
+ */
336
+ public function maybe_set_item_meta_data ( $ item , $ posted ) {
337
+ /**
338
+ * Call the parent method first to handle standard meta data
339
+ * This will populate the attribute key, eg: 'pa_color' or 'logo'
340
+ * BUT: if the attribute can be 'any' then we need to handle that
341
+ */
342
+ parent ::maybe_set_item_meta_data ( $ item , $ posted );
343
+
344
+ if ( ! $ item ->get_variation_id () || empty ( $ posted ['meta_data ' ] ) || ! is_array ( $ posted ['meta_data ' ] ) ) {
345
+ return ;
346
+ }
347
+
348
+ $ attributes = wc_get_product_variation_attributes ( $ item ->get_variation_id () );
349
+ $ product_id = $ item ->get_product_id ();
350
+ $ product = wc_get_product ( $ product_id );
351
+ $ parent_attributes = $ product ->get_attributes ();
352
+
353
+ foreach ( $ attributes as $ key => $ value ) {
354
+ if ( '' === $ value ) {
355
+ $ slug = str_replace ( 'attribute_ ' , '' , $ key );
356
+
357
+ if ( ! isset ( $ parent_attributes [ $ slug ] ) ) {
358
+ continue ;
359
+ }
360
+
361
+ $ name = $ parent_attributes [ $ slug ]['name ' ] ?? $ slug ;
362
+ if ( $ name === $ slug ) {
363
+ $ name = wc_attribute_label ( $ slug );
364
+ }
365
+
366
+ // find the value from $posted['meta_data']
367
+ foreach ( $ posted ['meta_data ' ] as $ meta ) {
368
+ // Match posted attribute label to the $name we just determined
369
+ if ( isset ( $ meta ['display_key ' ], $ meta ['display_value ' ] ) && $ meta ['display_key ' ] === $ name ) {
370
+ $ posted_value = $ meta ['display_value ' ];
371
+ // Only update if the posted value is non-empty
372
+ if ( $ posted_value ) {
373
+ $ item ->update_meta_data (
374
+ $ slug ,
375
+ $ posted_value ,
376
+ isset ( $ meta ['id ' ] ) ? $ meta ['id ' ] : ''
377
+ );
378
+ break ; // Stop searching once found
379
+ }
380
+ }
381
+ }
382
+ }
383
+ }
384
+ }
385
+
330
386
/**
331
387
* The way WooCommerce handles negative fees is ... weird.
332
388
* They by-pass the normal tax calculation, disregard the tax_status and tax_class, and apply the taxes to the fee line.
0 commit comments