Skip to content

Commit f5bab42

Browse files
refactor and changelog
1 parent 20c2154 commit f5bab42

File tree

2 files changed

+53
-38
lines changed

2 files changed

+53
-38
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: add
3+
4+
Implement Product Data Mapping for WooCommerce CLI Migrator

plugins/woocommerce/src/Internal/CLI/Migrator/Platforms/Shopify/ShopifyMapper.php

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,51 @@
2525
*/
2626
class ShopifyMapper implements PlatformMapperInterface {
2727

28+
/**
29+
* Shopify weight unit to standard unit mapping.
30+
*
31+
* @var array
32+
*/
33+
private const WEIGHT_UNIT_MAP = array(
34+
'GRAMS' => 'g',
35+
'KILOGRAMS' => 'kg',
36+
'POUNDS' => 'lb',
37+
'OUNCES' => 'oz',
38+
);
39+
40+
/**
41+
* Weight conversion factors between units.
42+
* Structure: [from_unit][to_unit] = factor
43+
*
44+
* @var array
45+
*/
46+
private const WEIGHT_CONVERSION_FACTORS = array(
47+
'kg' => array(
48+
'kg' => 1,
49+
'g' => 1000,
50+
'lb' => 2.20462,
51+
'oz' => 35.274,
52+
),
53+
'g' => array(
54+
'kg' => 0.001,
55+
'g' => 1,
56+
'lb' => 0.00220462,
57+
'oz' => 0.035274,
58+
),
59+
'lb' => array(
60+
'kg' => 0.453592,
61+
'g' => 453.592,
62+
'lb' => 1,
63+
'oz' => 16,
64+
),
65+
'oz' => array(
66+
'kg' => 0.0283495,
67+
'g' => 28.3495,
68+
'lb' => 0.0625,
69+
'oz' => 1,
70+
),
71+
);
72+
2873
/**
2974
* Fields to process during mapping.
3075
*
@@ -257,14 +302,7 @@ private function get_converted_weight( $weight, $weight_unit ): ?float {
257302
return null;
258303
}
259304

260-
$unit_map = array(
261-
'GRAMS' => 'g',
262-
'KILOGRAMS' => 'kg',
263-
'POUNDS' => 'lb',
264-
'OUNCES' => 'oz',
265-
);
266-
267-
$shopify_unit_key = $unit_map[ $weight_unit ] ?? null;
305+
$shopify_unit_key = self::WEIGHT_UNIT_MAP[ $weight_unit ] ?? null;
268306

269307
if ( ! $shopify_unit_key ) {
270308
return (float) $weight;
@@ -286,39 +324,12 @@ private function get_converted_weight( $weight, $weight_unit ): ?float {
286324
return is_numeric( $converted ) ? (float) $converted : null;
287325
}
288326

289-
// Fallback manual conversion.
290-
$conversion_factors = array(
291-
'kg' => array(
292-
'kg' => 1,
293-
'g' => 1000,
294-
'lb' => 2.20462,
295-
'oz' => 35.274,
296-
),
297-
'g' => array(
298-
'kg' => 0.001,
299-
'g' => 1,
300-
'lb' => 0.00220462,
301-
'oz' => 0.035274,
302-
),
303-
'lb' => array(
304-
'kg' => 0.453592,
305-
'g' => 453.592,
306-
'lb' => 1,
307-
'oz' => 16,
308-
),
309-
'oz' => array(
310-
'kg' => 0.0283495,
311-
'g' => 28.3495,
312-
'lb' => 0.0625,
313-
'oz' => 1,
314-
),
315-
);
316-
317-
if ( ! isset( $conversion_factors[ $shopify_unit_key ][ $store_weight_unit ] ) ) {
327+
// Fallback manual conversion using class constants.
328+
if ( ! isset( self::WEIGHT_CONVERSION_FACTORS[ $shopify_unit_key ][ $store_weight_unit ] ) ) {
318329
return (float) $weight;
319330
}
320331

321-
return (float) $weight * $conversion_factors[ $shopify_unit_key ][ $store_weight_unit ];
332+
return (float) $weight * self::WEIGHT_CONVERSION_FACTORS[ $shopify_unit_key ][ $store_weight_unit ];
322333
}
323334

324335
/**

0 commit comments

Comments
 (0)