Skip to main content
Shipping · Revision #4

How do I set a custom item weight totalizer for shipping rates?

How do I set a custom item weight totalizer for shipping rates?

Shipping rates can define a custom item weight totalizer to control how item weights are calculated when determining whether a rate is applicable.

For example, you may want certain product variants to be exempt from the shipping weight calculation.

First, add a setting to the variant:

\Aero\Catalog\Models\Variant::settings('shipping', function (\Aero\Common\Settings\ModelSettingGroup $group) {
    $group->boolean('is_weight_exempt')->default(false);
});

The setItemWeightTotalizer method can then exclude weight-exempt variants when calculating the total weight:

\Aero\Cart\Models\ShippingRate::setItemWeightTotalizer(function ($items) {
    return $items
        ->reject(function (\Aero\Cart\Models\OrderItem $item) {
            return $item->buyable->setting('shipping.is_weight_exempt');
        })
        ->sum(function (\Aero\Cart\Models\OrderItem $item) {
            return $item->buyable->weight;
        });
});

The totalizer receives the order items and the shipping rate as parameters and should return the total weight of the items or null.

Returning null means the default item weight totalizer will be used.


Parameters

Parameter Type Description
$items
\Aero\Common\Eloquent\JavaScriptableCollection
The order items being evaluated.
$rate
\Aero\Cart\Models\ShippingRate
The shipping rate being evaluated.