white-nl / commerce-sendcloud

Sendcloud integration for Craft Commerce
Other
5 stars 2 forks source link

Weight calculation is wrong when using grams #24

Closed jornwildenbeest closed 1 day ago

jornwildenbeest commented 1 day ago

The following code calculates the weight in KG:

private function _getOrderWeightInKg(Order $order): ?string
    {
        $weight = $order->getTotalWeight();
        if ($weight <= 0) {
            return null;
        }

        $weightUnit = Commerce::getInstance()->getSettings()->weightUnits;
        $totalWeight = match ($weightUnit) {
            'g' => $weight * 1000,
            'lb' => $weight * 0.453,
            default => $weight,
        };

        return (string)$totalWeight;
    }

But there is an miscalculation here.

If the weightUnit is grams (g). You want to divide by 1000, not multiply.

Explanation: if the weight is set to 850 Gram, to get KG's you need to divide by 1000.

The fix would be:

 $totalWeight = match ($weightUnit) {
            'g' => $weight / 1000,
            'lb' => $weight * 0.453,
            default => $weight,
        };
white-lukas commented 1 day ago

Hi @jornwildenbeest, Just tagged 4.0.1 with the fix