Feature: Order Updates, Events, Order Flows, Shipment And COD support
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Shipping\Support;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Sums ProductVariant::weight_value/weight_unit across a collection of
|
||||
* Cart/Order lines into a single kg figure — the same conversion table
|
||||
* Modules\Core\Shipping\Carriers\Acs\AcsRateDriver::totalWeightInKg()
|
||||
* already used privately for live rate quoting, now shared so
|
||||
* Modules\Core\Shipping\Extensions\OrderViewExtension's "Create Shipment"
|
||||
* weight default can compute the identical figure for an already-placed
|
||||
* Order instead of duplicating the unit table.
|
||||
*/
|
||||
class WeightCalculator
|
||||
{
|
||||
/**
|
||||
* @param Collection $lines Cart::$lines or Order::$lines, each with
|
||||
* its purchasable relation loaded (or loadable — ->load('purchasable')
|
||||
* is called here if not already eager-loaded).
|
||||
*/
|
||||
public static function totalKg(Collection $lines): float
|
||||
{
|
||||
$weight = 0.0;
|
||||
|
||||
foreach ($lines->load('purchasable') as $line) {
|
||||
$variant = $line->purchasable;
|
||||
|
||||
if (! $variant || ! $variant->weight_value) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$unit = $variant->weight_unit ?? 'kg';
|
||||
$value = (float) $variant->weight_value;
|
||||
|
||||
$weight += match ($unit) {
|
||||
'g' => $value / 1000,
|
||||
'lb' => $value * 0.45359237,
|
||||
'oz' => $value * 0.0283495231,
|
||||
default => $value, // kg
|
||||
} * $line->quantity;
|
||||
}
|
||||
|
||||
return $weight;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user