The official documentation that explains how to create shipping methods in Ibexa Commerce omits a crucial explanation: how to calculate the fees for the method in question?
So I'm going to fill this gap and explain how to create a fee 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 Symfony in general, this calculator will be a tagged service.
Here is the class src/Commerce/Attribute/String/StringStorageDefinition.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 if 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 shipping method configuration
$price = $method->getOptions()->get('price');
$currencyId = $method->getOptions()->get('currency');
$currency = $this->currencyService->getCurrency($currencyId);
// All items have free shipping, return a 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 configuration file config/services.yaml :
App\Commerce\Shipping\CostCalculator:
tags:
- name: ibexa.shipping.shipping.cost_calculator
method: custom_method # custom_method à remplacer par l'identifiant de la méthode de livraison
Si les assistants IA ont révolutionné la production de code en accélérant significativement le développement, ...
Vous en avez marre d'avoir les logs de vos jobs dans la base de données ...
Si vous développez sur Ibexa depuis quelques années, vous avez pris pour habitude de passer ...
Code Rhapsodie reprend la maintenance du package oauth2-apple de Patrick Bussmann. Ce fork garantit la ...
La suggestion de mots-clés de Taxonomy est disponible dans Ibexa DXP avec le pack de ...
Importing a very large SQL file can be problematic, sometimes causing your virtual machine (VM) ...
Sylius et Shopware sont deux outils (framework) permettant la réalisation d'un site e-commerce. Comment choisir ...
Ibexa published a security advisory and subsequently released new versions of Ibexa DXP v4.6.26 and ...
Connaissez-vous les Actions IA (AI Actions) d'Ibexa ? Les AI Actions, sont une solution extensible ...