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

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

For example, you may want sample products to be excluded from the item quantity used when evaluating quantity-based shipping rules.

First, define a setting on the variant:

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

The setItemQuantityTotalizer method can then exclude sample products when calculating the total quantity:

\Aero\Cart\Models\ShippingRate::setItemQuantityTotalizer(function ($items) {
    return $items
        ->reject(function (\Aero\Cart\Models\OrderItem $item) {
            return $item->buyable->setting('is_sample');
        })
        ->sum('quantity');
});

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

Returning null means that the default item quantity 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.

Revision #2
Created 2026-09-16 13:06:32 UTC by Ethan Payne
Updated 2026-10-02 12:59:53 UTC by Ethan Payne