SyliusCon 2025
Early Bird Deal
LogoLogo
🛣️ Roadmap💻 Sylius Demo💬 Community Slack
  • Sylius Documentation
  • Sylius Plugins
  • Sylius Stack
  • 📖Sylius 2.0 Documentation
    • Organization
      • Release Cycle
      • Backwards Compatibility Promise
      • Sylius Team
      • Sylius Roadmap
  • Getting Started with Sylius
    • Installation
    • Basic Configuration
    • Shipping & Payment
    • First Product
    • Customizing the Shop
    • Customizing Business Logic
    • Using API
    • Installing Plugins
    • Deployment
    • Summary
  • The Book
    • Introduction to Sylius
    • Installation
      • System Requirements
      • Sylius CE Installation
        • Sylius CE Installation with Docker
      • ➕Sylius Plus Installation
      • Upgrading Sylius CE
      • Upgrading Sylius Plus
    • Architecture
      • Architecture Overview
      • Architectural Drivers
      • Resource Layer
      • State Machine
      • Translations
      • E-Mails
      • Contact
      • Fixtures
      • Events
    • Configuration
      • Channels
      • Locales
      • Currencies
    • Customers
      • Customer & ShopUser
      • ➕Customer Pools
      • AdminUser
      • Addresses
        • Countries
        • Zones
        • Addresses
        • Address Book
    • Products
      • Products
      • Product Reviews
      • Product Associations
      • Attributes
      • Pricing
      • Catalog Promotions
      • Taxons
      • Inventory
      • ➕Multi-Source Inventory
      • Search
    • Carts & Orders
      • Orders
      • Cart flow
      • Taxation
      • Adjustments
      • Cart Promotions
      • Coupons
      • Payments
      • 🧩Invoices
      • Shipments
    • Support
    • Contributing
      • Contributing Code
        • Submitting a Patch
        • ⚠️Security Issues
        • Coding Standards
        • Conventions
        • Sylius License and Trademark
      • Contributing Translations
      • Key Contributors
  • The Customization Guide
    • Customizing Models
      • How to add a custom model?
      • How to add a custom translatable model?
    • Customizing Forms
      • How to add a live form for a custom model?
    • Customizing Styles
    • Customizing Validation
    • Customizing Menus
    • Customizing Templates
    • Customizing Translations
    • Customizing Flashes
    • Customizing State Machines
    • Customizing Grids
    • Customizing Fixtures
    • Customizing API
    • Customizing Serialization of API
    • Customizing Payments
      • How to integrate a Payment Gateway as a Plugin?
  • 🧑‍🍳The Cookbook
  • How to resize images?
  • How to add one image to an entity?
  • How to add multiple images to an entity?
  • Sylius 1.X Documentation
    • 📓Sylius 1.x Documentation
Powered by GitBook
LogoLogo

Developer

  • Community
  • Online Course

About

  • Team

© 2025 Sylius. All Rights Reserved

On this page
  • Custom Shipping Calculator
  • Step 1: Create the Custom Shipping Calculator
  • 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

Was this helpful?

Edit on GitHub
  1. Getting Started with Sylius

Customizing Business Logic

Sylius offers extensive customization options, allowing you to tailor your eCommerce store to your specific needs. Let’s walk through an example of how you can customize Sylius by implementing a custom shipping calculator.

Custom Shipping Calculator

A shipping calculator in Sylius calculates the shipping cost for an order. This calculation is usually based on the products and some configuration defined by the admin. Sylius provides two default calculators:

  • FlatRateCalculator: Charges a fixed rate per order.

  • PerUnitRateCalculator: Charges a rate based on the number of items in the order.

Let’s say you need to charge based on the number of parcels used to pack the order. You can create a custom calculator to achieve this.

Step 1: Create the Custom Shipping Calculator

Your custom calculator must implement the CalculatorInterface. Below is an example of a ParcelCalculator, which charges based on the number of parcels.

<?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';
    }
}

In this code, we calculate the number of parcels needed by dividing the total product units by the parcel size. The total shipping cost is then the number of parcels multiplied by the price per parcel.

Step 2: Register the Custom Calculator

To make this calculator available in the admin panel, register it as a service in your services.yaml:

# config/services.yaml
services:
    app.shipping_calculator.parcel:
        class: App\ShippingCalculator\ParcelCalculator
        tags:
            - { name: sylius.shipping_calculator }

This will allow your custom shipping calculator to be selected when configuring shipping methods in the admin panel.

Step 3: Configure the Shipping Method in the Admin Panel

Now that your calculator is registered, you can create a new Shipping Method in the admin panel using your ParcelCalculator. When setting up the shipping method, you will need to provide two configuration options:

  • Size: How many units fit into one parcel.

  • Price: The cost per parcel.

Testing the Custom Logic via API

Once everything is set up, you can test the logic through the API.

Add an Item to the Cart

Use the following API call to add an item to the cart:

curl --location --request PATCH 'https://your-shop-url.com/api/v2/shop/orders/CART_TOKEN/items' --header 'Content-Type: application/merge-patch+json' --data-raw '{
    "productVariant": "/api/v2/shop/product-variants/PRODUCT_VARIANT_CODE",
    "quantity": 1
}'

This should return a response with the cart, including the shippingTotal:

{
    "taxTotal": 0,
    "shippingTotal": 500,  // Shipping cost in cents
    "orderPromotionTotal": 0
}

The API returns amounts in the smallest currency unit (e.g., cents for USD). So 500 represents $5.00.

Change Item Quantity

You can modify the quantity of the item in the cart using the following API:

curl --location --request PATCH 'https://your-shop-url.com/api/v2/shop/orders/CART_TOKEN/items/ORDER_ITEM_ID' --header 'Content-Type: application/merge-patch+json' --data-raw '{
    "quantity": 4
}'

This should return a response with the updated shippingTotal:

{
    "taxTotal": 0,
    "shippingTotal": 1000,  // Updated shipping cost based on the new quantity
    "orderPromotionTotal": 0
}

Congratulations! You’ve now added custom business logic to your Sylius store by creating a custom shipping calculator. With this foundation, you can continue customizing your shop to suit your business needs.

PreviousCustomizing the ShopNextUsing API

Last updated 7 months ago

Was this helpful?

Configuration in the Admin Panel
Shipping cost for 1 pacel
Shipping cost for 4 parcels