Fatal error when calculating shipping for shipments with no set amount

Created on 30 September 2021, about 3 years ago
Updated 31 August 2023, over 1 year ago

Problem/Motivation

The docblock of \Drupal\commerce_shipping\Entity\ShipmentInterface::getAmount() states for the return value that it might be a Price object or NULL. \Drupal\commerce_google_tag_manager\EventTrackerService::calculateShipping() is however only assumming that a Price object is returned.

Steps to reproduce

Create an order without shippment costs, e.g. use the commerce_shipping module only for the shipping information and disable from the form display the shipping method during the checkout. After the payment step observe an exception thrown:

Error: Call to a member function getNumber() on null in Drupal\commerce_google_tag_manager\EventTrackerService->calculateShipping() (line 454 of modules/contrib/commerce_google_tag_manager/src/EventTrackerService.php).

Proposed resolution

Return the fallback 0 also if there is no amount on the shippment.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

RTBC

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany hchonov πŸ‡ͺπŸ‡ΊπŸ‡©πŸ‡ͺπŸ‡§πŸ‡¬

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • This is working for me as well.

  • πŸ‡ΊπŸ‡ΈUnited States lisastreeter

    I needed to refactor this patch to use one for the Support Google Analytics 4 issue #3190724 ✨ Support Google Analytics 4 Needs review . For some reason, "private" methods" were changed to "protected" methods as part of that issue, so patch #3 here does not apply.

    Posting an updated patch here for anybody else needing to make the switch to GA4 by the end of the month.

  • Status changed to Needs work over 1 year ago
  • πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia

    I agree that the adjusted amount is the one to use, but let's use the dedicated method ShipmentInterface::getAdjustedAmount(). Looking at the code of that method it appears that it is not sufficient to just add up the adjustments. The adjustments need to start from the base amount.

  • Status changed to RTBC over 1 year ago
  • πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia

    My previous comment was wrong. The base amount is actually added as an order adjustment, this is happening in LateOrderProcessor::process().

    I tested it and it is working perfectly. Thanks!

Production build 0.71.5 2024