Commit f0f431070d4 for woocommerce
commit f0f431070d4f9e65499bfe35b271d9ff9472396a
Author: Wesley Rosa <wesleyjrosa@gmail.com>
Date: Wed Jun 17 15:46:55 2026 -0300
Add WeightUnit enum class for woocommerce_weight_unit option values (#64422)
* Add TaxBasedOn enum class for woocommerce_tax_based_on option values
Co-authored-by: wjrosa <10187816+wjrosa@users.noreply.github.com>
Agent-Logs-Url: https://github.com/wjrosa/woocommerce/sessions/b2cc1cfd-3344-4125-abb6-16cf58408f16
* Add OrderItemType enum class for WooCommerce line item types
Co-authored-by: wjrosa <10187816+wjrosa@users.noreply.github.com>
Agent-Logs-Url: https://github.com/wjrosa/woocommerce/sessions/8c722ad0-cb4d-4315-94ff-0c609f9354b9
* Replace raw order item type strings with OrderItemType enum constants across src/
Co-authored-by: wjrosa <10187816+wjrosa@users.noreply.github.com>
Agent-Logs-Url: https://github.com/wjrosa/woocommerce/sessions/cd678828-7e57-48d9-a556-022005be84c4
* Add DefaultCustomerAddress enum class and implement across codebase
Co-authored-by: wjrosa <10187816+wjrosa@users.noreply.github.com>
Agent-Logs-Url: https://github.com/wjrosa/woocommerce/sessions/eb288e26-007e-4630-9484-9b989a0528f2
* Add changefile(s) from automation for the following project(s): woocommerce
* Remove cache priming addition from Orders REST API controller
* Remove unused OrderItemType import from Orders REST API controller
* Address review feedback on OrderItemType enum adoption
* Fix MDX compilation errors caused by HTML comments in docs
* Revert "Fix MDX compilation errors caused by HTML comments in docs"
This reverts commit 895c96aac0aafd4f9283b03abafe49d3eccc8526.
* Add changefile(s) from automation for the following project(s): woocommerce
* Replace raw order item type strings with OrderItemType enum constants across src/
Co-authored-by: wjrosa <10187816+wjrosa@users.noreply.github.com>
Agent-Logs-Url: https://github.com/wjrosa/woocommerce/sessions/cd678828-7e57-48d9-a556-022005be84c4
* Remove unused OrderItemType import from Orders REST API controller
* Address review feedback on OrderItemType enum adoption
* Fix MDX compilation errors caused by HTML comments in docs
* Revert "Fix MDX compilation errors caused by HTML comments in docs"
This reverts commit 895c96aac0aafd4f9283b03abafe49d3eccc8526.
* Add missing enum entries to README and use enum in MaxMind integration
- Add DefaultCustomerAddress, FeaturePluginCompatibility, and ProductTaxStatus
to the Enums README.md
- Fix PaymentGatewayFeatures → PaymentGatewayFeature (correct filename)
- Replace hardcoded 'geolocation'/'geolocation_ajax' strings in
class-wc-integration-maxmind-geolocation.php with DefaultCustomerAddress enum
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Fix array double arrow alignment lint warnings
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Remove manually created changelog entry
The PR is configured to auto-create the changelog entry on merge.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Revert unrelated README changes, keep only DefaultCustomerAddress entry
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Add changefile(s) from automation for the following project(s): woocommerce
* Remove duplicate changelog entry for tax-based-on enum
The 64104-trunk changelog already covers this change.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Making the TaxBasedOn enum class final
* Add WeightUnit enum class and adopt it across the codebase
* Use singular WeightUnit constants and migrate remaining literals
Rename the WeightUnit constants to singular form (KILOGRAM, GRAM, POUND,
OUNCE), bump the @since to 11.0.0, and replace remaining 'kg'/'g'/'oz'
string literals under src with the enum constants.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* Add WeightUnit::get_all() and use it for weight unit validation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: wjrosa <10187816+wjrosa@users.noreply.github.com>
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
Co-authored-by: Vladimir Reznichenko <kalessil@gmail.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/add-weight-unit-enum b/plugins/woocommerce/changelog/add-weight-unit-enum
new file mode 100644
index 00000000000..d5c7db79d85
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-weight-unit-enum
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Add WeightUnit enum class for woocommerce_weight_unit option values and implement it across the codebase.
diff --git a/plugins/woocommerce/includes/wc-formatting-functions.php b/plugins/woocommerce/includes/wc-formatting-functions.php
index da84e79affd..710c184f53b 100644
--- a/plugins/woocommerce/includes/wc-formatting-functions.php
+++ b/plugins/woocommerce/includes/wc-formatting-functions.php
@@ -8,6 +8,7 @@
* @version 2.1.0
*/
+use Automattic\WooCommerce\Enums\WeightUnit;
use Automattic\WooCommerce\Utilities\I18nUtil;
use Automattic\WooCommerce\Utilities\NumberUtil;
@@ -186,26 +187,26 @@ function wc_get_weight( $weight, $to_unit, $from_unit = '' ) {
// Unify all units to kg first.
if ( $from_unit !== $to_unit ) {
switch ( $from_unit ) {
- case 'g':
+ case WeightUnit::GRAM:
$weight *= 0.001;
break;
- case 'lbs':
+ case WeightUnit::POUND:
$weight *= 0.453592;
break;
- case 'oz':
+ case WeightUnit::OUNCE:
$weight *= 0.0283495;
break;
}
// Output desired unit.
switch ( $to_unit ) {
- case 'g':
+ case WeightUnit::GRAM:
$weight *= 1000;
break;
- case 'lbs':
+ case WeightUnit::POUND:
$weight *= 2.20462;
break;
- case 'oz':
+ case WeightUnit::OUNCE:
$weight *= 35.274;
break;
}
diff --git a/plugins/woocommerce/src/Enums/README.md b/plugins/woocommerce/src/Enums/README.md
index 2cb5243abd6..c4d04cc76c3 100644
--- a/plugins/woocommerce/src/Enums/README.md
+++ b/plugins/woocommerce/src/Enums/README.md
@@ -16,6 +16,7 @@ The enum classes make it easier to reference string values and avoid typos. They
- [ProductStockStatus](./ProductStockStatus.php) - Enumerates the possible stock statuses of a product.
- [ProductType](./ProductType.php) - Enumerates the possible types of a product.
- [TaxBasedOn](./TaxBasedOn.php) - Enumerates the possible values of the `woocommerce_tax_based_on` option.
+- [WeightUnit](./WeightUnit.php) - Enumerates the possible values of the `woocommerce_weight_unit` option.
## Contributing
diff --git a/plugins/woocommerce/src/Enums/WeightUnit.php b/plugins/woocommerce/src/Enums/WeightUnit.php
new file mode 100644
index 00000000000..7305b4eecb0
--- /dev/null
+++ b/plugins/woocommerce/src/Enums/WeightUnit.php
@@ -0,0 +1,57 @@
+<?php
+
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Enums;
+
+/**
+ * Enum class for the possible values of the `woocommerce_weight_unit` option,
+ * which determines the weight unit used throughout the store.
+ *
+ * @since 11.0.0
+ */
+final class WeightUnit {
+ /**
+ * Kilogram.
+ *
+ * @var string
+ */
+ public const KILOGRAM = 'kg';
+
+ /**
+ * Gram.
+ *
+ * @var string
+ */
+ public const GRAM = 'g';
+
+ /**
+ * Pound.
+ *
+ * @var string
+ */
+ public const POUND = 'lbs';
+
+ /**
+ * Ounce.
+ *
+ * @var string
+ */
+ public const OUNCE = 'oz';
+
+ /**
+ * Returns all weight unit values defined in this class.
+ *
+ * @since 11.0.0
+ *
+ * @return string[]
+ */
+ public static function get_all(): array {
+ return array(
+ self::KILOGRAM,
+ self::GRAM,
+ self::POUND,
+ self::OUNCE,
+ );
+ }
+}
diff --git a/plugins/woocommerce/src/Internal/CLI/Migrator/Platforms/Shopify/ShopifyMapper.php b/plugins/woocommerce/src/Internal/CLI/Migrator/Platforms/Shopify/ShopifyMapper.php
index 73dc10db006..5444369c697 100644
--- a/plugins/woocommerce/src/Internal/CLI/Migrator/Platforms/Shopify/ShopifyMapper.php
+++ b/plugins/woocommerce/src/Internal/CLI/Migrator/Platforms/Shopify/ShopifyMapper.php
@@ -9,6 +9,7 @@ declare( strict_types=1 );
namespace Automattic\WooCommerce\Internal\CLI\Migrator\Platforms\Shopify;
+use Automattic\WooCommerce\Enums\WeightUnit;
use Automattic\WooCommerce\Internal\CLI\Migrator\Interfaces\PlatformMapperInterface;
defined( 'ABSPATH' ) || exit;
@@ -31,10 +32,10 @@ class ShopifyMapper implements PlatformMapperInterface {
* @var array
*/
private const WEIGHT_UNIT_MAP = array(
- 'GRAMS' => 'g',
- 'KILOGRAMS' => 'kg',
+ 'GRAMS' => WeightUnit::GRAM,
+ 'KILOGRAMS' => WeightUnit::KILOGRAM,
'POUNDS' => 'lb',
- 'OUNCES' => 'oz',
+ 'OUNCES' => WeightUnit::OUNCE,
);
/**
@@ -44,29 +45,29 @@ class ShopifyMapper implements PlatformMapperInterface {
* @var array
*/
private const WEIGHT_CONVERSION_FACTORS = array(
- 'kg' => array(
- 'kg' => 1,
- 'g' => 1000,
- 'lb' => 2.20462,
- 'oz' => 35.274,
+ WeightUnit::KILOGRAM => array(
+ WeightUnit::KILOGRAM => 1,
+ WeightUnit::GRAM => 1000,
+ 'lb' => 2.20462,
+ WeightUnit::OUNCE => 35.274,
),
- 'g' => array(
- 'kg' => 0.001,
- 'g' => 1,
- 'lb' => 0.00220462,
- 'oz' => 0.035274,
+ WeightUnit::GRAM => array(
+ WeightUnit::KILOGRAM => 0.001,
+ WeightUnit::GRAM => 1,
+ 'lb' => 0.00220462,
+ WeightUnit::OUNCE => 0.035274,
),
- 'lb' => array(
- 'kg' => 0.453592,
- 'g' => 453.592,
- 'lb' => 1,
- 'oz' => 16,
+ 'lb' => array(
+ WeightUnit::KILOGRAM => 0.453592,
+ WeightUnit::GRAM => 453.592,
+ 'lb' => 1,
+ WeightUnit::OUNCE => 16,
),
- 'oz' => array(
- 'kg' => 0.0283495,
- 'g' => 28.3495,
- 'lb' => 0.0625,
- 'oz' => 1,
+ WeightUnit::OUNCE => array(
+ WeightUnit::KILOGRAM => 0.0283495,
+ WeightUnit::GRAM => 28.3495,
+ 'lb' => 0.0625,
+ WeightUnit::OUNCE => 1,
),
);
@@ -298,7 +299,7 @@ class ShopifyMapper implements PlatformMapperInterface {
$store_weight_unit = get_option( 'woocommerce_weight_unit' );
- if ( 'lbs' === $store_weight_unit ) {
+ if ( WeightUnit::POUND === $store_weight_unit ) {
$store_weight_unit = 'lb';
}
diff --git a/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Products/Controller.php b/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Products/Controller.php
index ab241c7ae89..3f952b8360d 100644
--- a/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Products/Controller.php
+++ b/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Products/Controller.php
@@ -17,6 +17,7 @@ use Automattic\WooCommerce\Enums\ProductStockStatus;
use Automattic\WooCommerce\Enums\ProductTaxStatus;
use Automattic\WooCommerce\Enums\ProductType;
use Automattic\WooCommerce\Enums\CatalogVisibility;
+use Automattic\WooCommerce\Enums\WeightUnit;
use Automattic\WooCommerce\Internal\CostOfGoodsSold\CogsAwareRestControllerTrait;
use Automattic\WooCommerce\Internal\Utilities\ProductUtil;
use Automattic\WooCommerce\Utilities\I18nUtil;
@@ -1456,7 +1457,7 @@ class Controller extends WC_REST_Products_V2_Controller {
* @return array
*/
public function get_item_schema() {
- $weight_unit_label = I18nUtil::get_weight_unit_label( get_option( 'woocommerce_weight_unit', 'kg' ) );
+ $weight_unit_label = I18nUtil::get_weight_unit_label( get_option( 'woocommerce_weight_unit', WeightUnit::KILOGRAM ) );
$dimension_unit_label = I18nUtil::get_dimensions_unit_label( get_option( 'woocommerce_dimension_unit', 'cm' ) );
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
diff --git a/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Settings/Products/Controller.php b/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Settings/Products/Controller.php
index 940809dcb08..bb5027945bb 100644
--- a/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Settings/Products/Controller.php
+++ b/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Settings/Products/Controller.php
@@ -12,6 +12,7 @@ declare( strict_types=1 );
namespace Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Products;
use WP_Error;
+use Automattic\WooCommerce\Enums\WeightUnit;
use Automattic\WooCommerce\Internal\RestApi\Routes\V4\AbstractController;
use Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Products\Schema\ProductSettingsSchema;
use WP_REST_Server;
@@ -243,7 +244,7 @@ class Controller extends AbstractController {
*
* @param array $weight_units Array of weight unit strings.
*/
- $valid_units = apply_filters( 'woocommerce_weight_units', array( 'kg', 'g', 'lbs', 'oz' ) );
+ $valid_units = apply_filters( 'woocommerce_weight_units', WeightUnit::get_all() );
if ( ! in_array( $value, $valid_units, true ) ) {
return new WP_Error(
'rest_invalid_param',
diff --git a/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Settings/Products/Schema/ProductSettingsSchema.php b/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Settings/Products/Schema/ProductSettingsSchema.php
index a19be7a0e1d..66eb42efa6d 100644
--- a/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Settings/Products/Schema/ProductSettingsSchema.php
+++ b/plugins/woocommerce/src/Internal/RestApi/Routes/V4/Settings/Products/Schema/ProductSettingsSchema.php
@@ -13,6 +13,7 @@ namespace Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Products\Sc
defined( 'ABSPATH' ) || exit;
+use Automattic\WooCommerce\Enums\WeightUnit;
use Automattic\WooCommerce\Internal\RestApi\Routes\V4\AbstractSchema;
use WP_REST_Request;
@@ -252,10 +253,10 @@ class ProductSettingsSchema extends AbstractSchema {
switch ( $field_id ) {
case 'woocommerce_weight_unit':
return array(
- 'kg' => __( 'kg', 'woocommerce' ),
- 'g' => __( 'g', 'woocommerce' ),
- 'lbs' => __( 'lbs', 'woocommerce' ),
- 'oz' => __( 'oz', 'woocommerce' ),
+ WeightUnit::KILOGRAM => __( 'kg', 'woocommerce' ),
+ WeightUnit::GRAM => __( 'g', 'woocommerce' ),
+ WeightUnit::POUND => __( 'lbs', 'woocommerce' ),
+ WeightUnit::OUNCE => __( 'oz', 'woocommerce' ),
);
case 'woocommerce_dimension_unit':
diff --git a/plugins/woocommerce/src/StoreApi/Schemas/V1/CartSchema.php b/plugins/woocommerce/src/StoreApi/Schemas/V1/CartSchema.php
index 21a2c257bdc..137b3e39f1a 100644
--- a/plugins/woocommerce/src/StoreApi/Schemas/V1/CartSchema.php
+++ b/plugins/woocommerce/src/StoreApi/Schemas/V1/CartSchema.php
@@ -1,6 +1,7 @@
<?php
namespace Automattic\WooCommerce\StoreApi\Schemas\V1;
+use Automattic\WooCommerce\Enums\WeightUnit;
use Automattic\WooCommerce\Internal\Tax\TaxRateDataStore;
use Automattic\WooCommerce\Internal\Utilities\ProductUtil;
use Automattic\WooCommerce\StoreApi\SchemaController;
@@ -370,7 +371,7 @@ class CartSchema extends AbstractSchema {
'has_calculated_shipping' => $cart->has_calculated_shipping(),
'shipping_rates' => $this->get_item_responses_from_schema( $this->shipping_rate_schema, $shipping_packages ),
'items_count' => $cart->get_cart_contents_count(),
- 'items_weight' => wc_get_weight( $cart->get_cart_contents_weight(), 'g' ),
+ 'items_weight' => wc_get_weight( $cart->get_cart_contents_weight(), WeightUnit::GRAM ),
'cross_sells' => $this->get_item_responses_from_schema( $this->cross_sells_item_schema, $cross_sells ),
'errors' => $cart_errors,
'payment_methods' => array_values( wp_list_pluck( WC()->payment_gateways->get_available_payment_gateways(), 'id' ) ),