Customizing Business Logic
Custom Shipping Calculator
Step 1: Create the Custom Shipping Calculator
<?php
# src/ShippingCalculator/ParcelCalculator.php
declare(strict_types=1);
namespace App\ShippingCalculator;
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
use Sylius\Component\Shipping\Model\ShipmentInterface;
final class ParcelCalculator implements CalculatorInterface
{
public function calculate(ShipmentInterface $subject, array $configuration): int
{
$parcelSize = $configuration['size'];
$parcelPrice = $configuration['price'];
$numberOfPackages = ceil($subject->getUnits()->count() / $parcelSize);
return (int) ($numberOfPackages * $parcelPrice);
}
public function getType(): string
{
return 'parcel';
}
}Step 2: Register the Custom Calculator
Step 3: Configure the Shipping Method in the Admin Panel



Testing the Custom Logic via API
Add an Item to the Cart
Change Item Quantity
Last updated
Was this helpful?
