A new interesting addition coming in 1.2.0-rc3 (< 2 weeks) is the ability to add extra fees to an item that are added into the subtotal (before discount/tax calculations) through Item Price Adjustments.
Price adjustments aren’t entirely new; they already existed for discounts. That’s now been extended for price increases as well with a new comOrderItemExtraAdjustment
class.
This is especially useful for things like configurators where the customer can select options which (through a custom add to cart snippet as this topic discusses) are added to an item. Also things like mandatory per-item insurance could be done with this. One client use case that triggered it was being able of charging an extra fee for gift wrapping specific items.
There’s 3 options. A per-item extra fee, a per-item-quantity fee, or a percentage fee. Given a comOrderItem $item
object, here’s some example code on how that would work:
// Adding a fixed fee only applied once, regardless of quantity
$fixed = $this->adapter->newObject('comOrderItemExtraAdjustment');
$fixed->fromArray([
'key' => 'some-fixed-fee',
'name' => 'Some fixed fee',
'price_change' => 250,
'price_change_per_quantity' => false,
'show_on_order' => true,
]);
$item->addPriceAdjustment($fixed);
// Adding a fixed fee, once per item quantity
$perItem = $this->adapter->newObject('comOrderItemExtraAdjustment');
$perItem->fromArray([
'key' => 'adj-per-quantity',
'name' => 'Gift wrapping (cost per item)',
'price_change' => 150,
'price_change_per_quantity' => true,
'show_on_order' => true,
]);
$item->addPriceAdjustment($perItem);
// Adding a percentage fee
$percentage = $this->adapter->newObject('comOrderItemExtraAdjustment');
$percentage->fromArray([
'key' => 'adj-percentage',
'name' => 'Insurance',
'price_change_percentage' => 2.25,
'show_on_order' => true,
]);
$item->addPriceAdjustment($percentage);
Setting show_for_order
makes sure you can access it in the front-end template through item.adjustments
.
The default frontend/checkout/cart/items.twig template has been adjusted with the following which can serve as an example of how to show these types of extra fees. First it filters it only on extra adjustments (so discounts, i.e. coupons, are not shown here - discounts are rendered differently, typically) and then it shows the name of the adjustment and the total impact it has on the item price.
{% set adjustments = item.adjustments|filter(v => v.type == 'extra') %}
{% if adjustments|length > 0 %}
{% for adjustment in adjustments %}
<br>
+ {{ adjustment.name }}
({{ adjustment.total_change_formatted }})
{% endfor %}
{% endif %}