How do I set a custom order validator for shipping rates?

You can override the default logic that decides whether a shipping rate is applicable to an order. By default shipping rates are validated against their configuration set in the admin (allowed/disallowed tags, total weight, and total price).

To set the logic you need to use the \Aero\Cart\Models\ShippingRate::setOrderValidator() method. This expects you to pass a closure that will return true, false, or null (if null is returned then the default shipping rate logic is used). The closure will be passed the shipping rate and the order to validate.

In this example code if the shipping rate price is free then the shipping rate will be invalid unless the current day is Monday. If the shipping rate price is not free then null is returned so that the normal shipping rate validation code runs.

<?php

namespace Acme\MyModule;

use Aero\Cart\Models\Order;
use Aero\Cart\Models\ShippingRate;
use Aero\Common\Providers\ModuleServiceProvider;
use Carbon\Carbon;

class ServiceProvider extends ModuleServiceProvider
{
   public function setup()
   {
       ShippingRate::setOrderValidator(function (ShippingRate $rate, Order $order) {
           if ($rate->price_inc === 0) {
               return Carbon::now()->isDayOfWeek(1);
           }

           return null;
       });
   }
}

Articles in this section

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