Skip to content

Commit 1005178

Browse files
committed
Fix: issue where variant was not saving properly in the Order Item meta data
1 parent 4ad2a53 commit 1005178

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

includes/API/Orders_Controller.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,62 @@ public function prepare_line_items( $posted, $action = 'create', $item = null )
327327
return $item;
328328
}
329329

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+
330386
/**
331387
* The way WooCommerce handles negative fees is ... weird.
332388
* They by-pass the normal tax calculation, disregard the tax_status and tax_class, and apply the taxes to the fee line.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wcpos/woocommerce-pos",
3-
"version": "1.7.5",
3+
"version": "1.7.6",
44
"description": "A simple front-end for taking WooCommerce orders at the Point of Sale.",
55
"main": "index.js",
66
"workspaces": {

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: kilbot
33
Tags: ecommerce, point-of-sale, pos, inventory, woocommerce
44
Requires at least: 5.6
55
Tested up to: 6.8
6-
Stable tag: 1.7.5
6+
Stable tag: 1.7.6
77
License: GPL-3.0
88
License URI: http://www.gnu.org/licenses/gpl-3.0.html
99

@@ -88,6 +88,9 @@ There is more information on our website at [https://wcpos.com](https://wcpos.co
8888

8989
== Changelog ==
9090

91+
= 1.7.6 - 2025/04/14 =
92+
* Fix: issue where variant was not saving properly in the Order Item meta data
93+
9194
= 1.7.5 - 2025/04/09 =
9295
* Fix: $object->object_type error, use $object->get_type() instead
9396
* Fix: increase woocommerce_get_checkout_order_received_url to ensure POS Thank You page is used for POS orders

woocommerce-pos.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: WooCommerce POS
44
* Plugin URI: https://wordpress.org/plugins/woocommerce-pos/
55
* Description: A simple front-end for taking WooCommerce orders at the Point of Sale. Requires <a href="http://wordpress.org/plugins/woocommerce/">WooCommerce</a>.
6-
* Version: 1.7.5
6+
* Version: 1.7.6
77
* Author: kilbot
88
* Author URI: http://wcpos.com
99
* Text Domain: woocommerce-pos
@@ -24,12 +24,12 @@
2424
namespace WCPOS\WooCommercePOS;
2525

2626
// Define plugin constants.
27-
const VERSION = '1.7.5';
27+
const VERSION = '1.7.6';
2828
const PLUGIN_NAME = 'woocommerce-pos';
2929
const SHORT_NAME = 'wcpos';
3030
\define( __NAMESPACE__ . '\PLUGIN_FILE', plugin_basename( __FILE__ ) ); // 'woocommerce-pos/woocommerce-pos.php'
3131
\define( __NAMESPACE__ . '\PLUGIN_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
32-
\define( __NAMESPACE__ . '\PLUGIN_URL', trailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
32+
\define( __NAMESPACE__ . '\PLUGIN_URL', trailingslashit( plugin_dir_url( __FILE__ ) ) );
3333

3434
// Minimum requirements.
3535
const WC_MIN_VERSION = '5.3';

0 commit comments

Comments
 (0)