Category: Development

How to setup a Drupal Commerce Order total Range condition

Author(s)

Kevin
Kevin
The other day, I faced a serious problem. One of our clients wanted a new shipping methods that must be applied on Ranged Order Total Price condition.
AKA a shipping method that must apply only when the order total falls within a range. E.g. $100 => order total <= $200.
After digging the Drupal Commerce Issue Queue, I found out there is no out-of-the-box solution/patch (for now) to apply an Order Total Price within a range Condition. So let's code it !
This is what you will be able to achieve at the end of this story
“ Truth can only be found in one place: the code ”

Robert C. Martin

Clean Code: A Handbook of Agile Software Craftsmanship

The Plugin Condition

For the most curious ones, here is a unit test Class that you may be interested to add on your project to ensure plugin stability: https://gist.github.com/WengerK/edbe17bb52067c5be86f34d1d1d2bd4e#file-ordertotalpricerangetest-php.
“ Well done you did it ! ”

Shia LaBeouf

That's pretty much all the hocus-pocus that you need to have custom Commerce Total Price Order Condition fitting within a range.

Going Further

The code upper here is simplified.
Still, you may want to change the code in order to exclude adjustments or change the in between operators used.
Total price includes adjustments
You may want to remove the adjustments (taxes, shipping, etc ...) in order to trigger the condition.
Then update the OrderTotalPriceRange::evalute method.
// (...)

if (!$total_price) {
  return FALSE;
}

// Subtract tax & shipping adjustments.
$adjustments = $order->collectAdjustments(array_filter(['tax', 'shipping']));
foreach ($adjustments as $adjustment) {
  $total_price = $total_price->subtract($adjustment->getAmount());
}

// (...)
Configurable between operators
The given plugin use a hardcoded “Between” condition including the input values (>= && <=).
You can still change this condition by updating the return condition of OrderTotalPriceRange::evalute.
return $total_price->greaterThan($condition_price_from)
      && $total_price->lessThan($condition_price_to);

Credits

Many thanks to Quentin Fahrner for sharing with me an inital Plugin solution.

Sources

For the most curious of you, here are some sources of additional information that inspired the creation of this article.
Quentin Fahrner (11 May, 2022). Order total range condition.
See on https://www.drupal.org/project/commerce/issues/2938731

We are ready to assist you with your Drupal project

More articles

Author(s)

Akira
Akira

Making of : “La nuit des Losers”

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.
lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.
Making of : “La nuit des Losers”

Author(s)

Pierre
Pierre

Dark mode: necessity or trend

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.
Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.
Dark mode: necessity or trend

Author(s)

Yann
Pierre
Yann and Pierre

Experiment #2 – Surway, a social experiments game

Pour notre deuxième expérimentation, nous avons relevé le défi de sortir de notre zone de confort en développant Surway, un jeu vidéo jouable directement dans le navigateur, où les actions de chaque joueur restent inscrites pour tous ceux qui suivront.
Pour notre deuxième expérimentation, nous avons relevé le défi de sortir de notre zone de confort en développant Surway, un jeu vidéo jouable directement dans le navigateur, où les actions de chaque joueur restent inscrites pour tous ceux qui suivront.
Experiment #2 – Surway, a social experiments game