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.
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
:
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:
This should return a response with the cart, including the shippingTotal
:
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:
This should return a response with the updated shippingTotal
:
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.
Last updated