How do I use a custom total item weight totalizer for a shipping rate?

Setting a custom item weight totalizer allows you to define the logic used to get the total weight of a collection of order items (this value will be used when validating that a shipping rate is available for an order).

To set the logic you need to use the \Aero\Cart\Models\ShippingRate::setItemWeightTotalizer() method. This method expects you to pass a closure that will return a number for the total price or null (if null is returned then the default total weight logic is used). The closure will be passed the order items and the shipping rate.

In this example code if an order item weighs less than 50 then the item will not be used in the calculation for the total weight of the order items.

<?php

namespace Acme\MyModule;

use Aero\Cart\Models\OrderItem;
use Aero\Cart\Models\ShippingRate;
use Aero\Common\Providers\ModuleServiceProvider;
use Illuminate\Support\Collection;

class ServiceProvider extends ModuleServiceProvider
{
   public function setup()
   {
       ShippingRate::setItemWeightTotalizer(function (Collection $items, ShippingRate $rate) {
           $items = $items->filter(function (OrderItem $item) {
               return $item->total_weight <= 50;
           });

           return $items->sum('total_weight');
       });
   }
}

Articles in this section

Was this article helpful?
0 out of 0 found this helpful