The official documentation that explains how to create shipping methods in Ibexa Commerce omits a crucial explanation: how to calculate the costs for the method in question.
So I'm going to fill this gap and explain how to create a cost calculator for a shipping method. I'll take as an example a simple case where we have products with free shipping:
Like many other things in Ibexa, or in Symfony in general, this calculator will be a tagged service.
Here is the class src/Commerce/Shipping/CostCalculator.php :
<?php
declare(strict_types=1);
namespace App\Commerce\Shipping;
use Ibexa\Contracts\Cart\Value\CartInterface;
use Ibexa\Contracts\ProductCatalog\CurrencyServiceInterface;
use Ibexa\Contracts\ProductCatalog\Values\AttributeInterface;
use Ibexa\Contracts\Shipping\ShippingMethod\CostCalculatorInterface;
use Ibexa\Contracts\Shipping\Value\ShippingMethod\ShippingMethodInterface;
use Ibexa\ProductCatalog\Money\DecimalMoneyFactory;
use Money\Currency;
use Money\Money;
class CostCalculator implements CostCalculatorInterface
{
public function __construct(
private readonly DecimalMoneyFactory $decimalMoneyFactory,
private readonly CurrencyServiceInterface $currencyService
) {
}
public function calculate(ShippingMethodInterface $method, CartInterface $cart): Money
{
$isFree = true;
// Check for each item in the cart whether it has free shipping
foreach ($cart->getEntries() as $entry) {
/** @var array<AttributeInterface> $attributes */
$attributes = iterator_to_array($entry->getProduct()->getAttributes());
if (!$attributes['free_shipping']->getValue()) {
$isFree = false;
break;
}
}
// Get the shipping method configuration
$price = $method->getOptions()->get('price');
$currencyId = $method->getOptions()->get('currency');
$currency = $this->currencyService->getCurrency($currencyId);
// All items have free shipping, return an amount of 0
if ($isFree) {
return new Money('0', new Currency($currency->getCode()));
}
return $this->decimalMoneyFactory->getMoneyParser()->parse(
$price,
new Currency($currency->getCode())
);
}
}
All that remains is to tag our service in the config/services.yaml configuration file:
App\Commerce\Shipping\CostCalculator:
tags:
- name: ibexa.shipping.shipping.cost_calculator
method: custom_method # replace custom_method with the identifier of the shipping method
Since version 5.0.8, Ibexa DXP ships an MCP (Model Context Protocol) server that lets an ...
Cadeaux.com saw its Valentine's Day outperform Christmas with half as many servers. A look back ...
Since our last article, Ibexa Dataflow has gone from version 6.1.2 to 7.0.0: a richer ...
New dashboard, new design, new analyses and an ambitious roadmap: Security Audit moves to version ...
While AI assistants have revolutionised code production by significantly speeding up development, their widespread use ...
Fed up with having your job logs in the database? Even more fed up with ...
If you have been developing on Ibexa for a few years, you have probably got ...
Code Rhapsodie is taking over maintenance of Patrick Bussmann's oauth2-apple package. This fork ensures the ...
Taxonomy keyword suggestion is available in Ibexa DXP with the LTS update pack and the ...