Automattic\WooCommerce\Internal\CLI\Migrator\Platforms\Shopify
ShopifyMapper::get_converted_weight
Converts weight based on Shopify weight unit to store's weight unit.
Method of the class: ShopifyMapper{}
No Hooks.
Returns
float|null. The converted weight, or null if input is invalid/zero.
Usage
// private - for code of main (parent) class only $result = $this->get_converted_weight( $weight, $weight_unit ): ?float;
- $weight(float|null) (required)
- The weight value from Shopify.
- $weight_unit(string|null) (required)
- The weight unit from Shopify.
ShopifyMapper::get_converted_weight() ShopifyMapper::get converted weight code WC 10.8.1
private function get_converted_weight( $weight, $weight_unit ): ?float {
if ( null === $weight || null === $weight_unit || (float) $weight <= 0 ) {
return null;
}
$shopify_unit_key = self::WEIGHT_UNIT_MAP[ $weight_unit ] ?? null;
if ( ! $shopify_unit_key ) {
return (float) $weight;
}
$store_weight_unit = get_option( 'woocommerce_weight_unit' );
if ( 'lbs' === $store_weight_unit ) {
$store_weight_unit = 'lb';
}
if ( $shopify_unit_key === $store_weight_unit ) {
return (float) $weight;
}
// Use wc_get_weight for conversion if possible.
if ( function_exists( 'wc_get_weight' ) ) {
$converted = wc_get_weight( (float) $weight, $store_weight_unit, $shopify_unit_key );
return is_numeric( $converted ) ? (float) $converted : null;
}
// Fallback manual conversion using class constants.
if ( ! isset( self::WEIGHT_CONVERSION_FACTORS[ $shopify_unit_key ][ $store_weight_unit ] ) ) {
return (float) $weight;
}
return (float) $weight * self::WEIGHT_CONVERSION_FACTORS[ $shopify_unit_key ][ $store_weight_unit ];
}