Incorrect Shipping Fee Calculation in Percentage of Order Value Shipping Method

Created on 18 September 2024, 4 months ago
Updated 20 September 2024, 3 months ago

Problem/Motivation

The Percentage of Order Value shipping method in Drupal Commerce was calculating shipping fees inaccurately. Administrators set a specific percentage to charge based on the order subtotal, but the applied shipping rates did not reflect the intended percentage, leading to inconsistent shipping costs and potential customer dissatisfaction.

Steps to reproduce

1. Enable the Module:

  • Activate the commerce_shipping_order_percentage custom module via the Drupal admin interface.

2. Configure Shipping Method:

  • Navigate to Commerce > Configuration > Shipping > Shipping methods.
  • Add or edit the Percentage of Order Value shipping method.
  • Set a desired percentage (e.g., 10%) for shipping charges

3. Place an Order:

  • Create an order with a known subtotal.

4. Verify Shipping Charge:

  • Proceed to checkout and observe the applied shipping fee.
  • Compare the shipping fee against the expected percentage of the order subtotal.

5. Identify Discrepancy:
Notice that the shipping fee does not match the configured percentage, indicating a calculation error.

Proposed resolution

The issue was resolved by refining the shipping rate calculation logic within the RateLookupService. Key actions included:

1. Corrected Calculation Formula:
• Ensured shipping fees are accurately calculated as the specified percentage of the order subtotal:

$shipping_total_amount = ($subtotal_number * $percentage_charge) / 100;

2. Configuration Consistency:
• Aligned configuration settings to ensure the entered percentage is correctly applied.
• Moved rate_label outside the shipping_settings array for consistency.

3. Property Declaration Alignment:
• Removed type declarations from the $services property in the PercentageOfOrderValue class to match the parent ShippingMethodBase class, preventing type conflicts.

🐛 Bug report
Status

Postponed: needs info

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @matt.hood
  • 🇳🇿New Zealand andyd328 Lyttelton, NZ 🇳🇿

    Thanks for the report Matt.

    It's working exactly how I expect here. When I have a min of $20 and a max of $150 it uses the given % inside that range and the appropriate max/min outside it.
    Maybe there is a difference in the way taxes are set up or displayed, or something in one of our set ups that's affecting the output?

    If you'd like to upload a patch or create a fork I'm happy to have a look and see what the differences are.

  • Status changed to Postponed: needs info 4 months ago
  • 🇳🇿New Zealand andyd328 Lyttelton, NZ 🇳🇿
  • Thanks for the quick followup Andy, I'll attach a screenshot to show what is happened on my end. The order percentage:

    Order Percentage Config (10%):

    Miscalculation for order %:

    It looks like it calculates the origanal percentage for shipping which is correct but then it calculates the shipping again on the review step which is 10% of the total on the previous step (which already had the original 10% added to it).

    I have a patch for the ecommerce site I have this running on but - it is not the best solution. I'll upload it but will also be looking into a better solutions in the upcoming week.

    • andyd328 committed c2ce7e10 on 1.0.x
      Issue #3475406 by matt.hood,andyd328: use subtotal to calculate order...
  • 🇳🇿New Zealand andyd328 Lyttelton, NZ 🇳🇿

    Changed order calc to use getSubtotalPrice instead of getTotalPrice.
    Thanks matt.hood!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024