Call to a member function getCurrencyCode() on null

Created on 31 July 2024, 6 months ago
Updated 2 August 2024, 6 months ago

Problem/Motivation

Error: Call to a member function getCurrencyCode() on null in Drupal\commerce_shipping\Plugin\Commerce\PromotionOffer\ShipmentFixedAmountOff->applyToShipment() (Zeile 28 in /var/www/vhosts/example.com/www10/modules/contrib/commerce_shipping/src/Plugin/Commerce/PromotionOffer/ShipmentFixedAmountOff.php).

#0 /var/www/vhosts/example.com/modules/contrib/commerce_shipping/src/Plugin/Commerce/PromotionOffer/ShipmentPromotionOfferBase.php(191): Drupal\commerce_shipping\Plugin\Commerce\PromotionOffer\ShipmentFixedAmountOff->applyToShipment()
#1 /var/www/vhosts/example.com/modules/contrib/commerce/modules/promotion/src/Entity/Promotion.php(624): Drupal\commerce_shipping\Plugin\Commerce\PromotionOffer\ShipmentPromotionOfferBase->apply()
#2 /var/www/vhosts/example.com/modules/contrib/commerce/modules/promotion/src/PromotionOrderProcessor.php(139): Drupal\commerce_promotion\Entity\Promotion->apply()
#3 /var/www/vhosts/example.com/modules/contrib/commerce/modules/order/src/OrderRefresh.php(198): Drupal\commerce_promotion\PromotionOrderProcessor->process()
#4 /var/www/vhosts/example.com/modules/contrib/commerce/modules/order/src/OrderStorage.php(123): Drupal\commerce_order\OrderRefresh->refresh()
#5 /var/www/vhosts/example.com/modules/contrib/commerce/modules/order/src/OrderStorage.php(86): Drupal\commerce_order\OrderStorage->doOrderPreSave()
#6 /var/www/vhosts/example.com/core/lib/Drupal/Core/Entity/EntityStorageBase.php(529): Drupal\commerce_order\OrderStorage->invokeHook()
#7 /var/www/vhosts/example.com/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(753): Drupal\Core\Entity\EntityStorageBase->doPreSave()
#8 /var/www/vhosts/example.com/core/lib/Drupal/Core/Entity/EntityStorageBase.php(483): Drupal\Core\Entity\ContentEntityStorageBase->doPreSave()
#9 /var/www/vhosts/example.com/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(806): Drupal\Core\Entity\EntityStorageBase->save()
#10 /var/www/vhosts/example.com/modules/contrib/commerce/modules/order/src/OrderStorage.php(169): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save()
#11 /var/www/vhosts/example.com/core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\commerce_order\OrderStorage->save()
#12 /var/www/vhosts/example.com/modules/contrib/commerce_shipping/src/Plugin/Commerce/CheckoutPane/ShippingInformation.php(349): Drupal\Core\Entity\EntityBase->save()
#13 /var/www/vhosts/example.com/modules/contrib/commerce/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowWithPanesBase.php(546): Drupal\commerce_shipping\Plugin\Commerce\CheckoutPane\ShippingInformation->buildPaneForm()
#14 [internal function]: Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowWithPanesBase->buildForm()
#15 /var/www/vhosts/example.com/core/lib/Drupal/Core/Form/FormBuilder.php(536): call_user_func_array()
#16 /var/www/vhosts/example.com/core/lib/Drupal/Core/Form/FormBuilder.php(284): Drupal\Core\Form\FormBuilder->retrieveForm()
#17 /var/www/vhosts/example.com/core/lib/Drupal/Core/Form/FormBuilder.php(224): Drupal\Core\Form\FormBuilder->buildForm()
#18 /var/www/vhosts/example.com/modules/contrib/commerce/modules/checkout/src/Controller/CheckoutController.php(143): Drupal\Core\Form\FormBuilder->getForm()
#19 [internal function]: Drupal\commerce_checkout\Controller\CheckoutController->formPage()
#20 /var/www/vhosts/example.com/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
#21 /var/www/vhosts/example.com/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#22 /var/www/vhosts/example.com/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext()
#23 /var/www/vhosts/example.com/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
#24 /var/www/vhosts/example.com/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#25 /var/www/vhosts/example.com/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
#26 /var/www/vhosts/example.com/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle()
#27 /var/www/vhosts/example.com/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
#28 /var/www/vhosts/example.com/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
#29 /var/www/vhosts/example.com/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\ContentLength->handle()
#30 /var/www/vhosts/example.com/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
#31 /var/www/vhosts/example.com/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
#32 /var/www/vhosts/example.com/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
#33 /var/www/vhosts/example.com/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
#34 /var/www/vhosts/example.com/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
#35 /var/www/vhosts/example.com/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
#36 /var/www/vhosts/example.com/index.php(19): Drupal\Core\DrupalKernel->handle()
#37 {main}

Steps to reproduce

Drupal 10.1.3, Commerce 8.x-2.39, Commerce Shipping 8.x-2.9+3-dev

Any advice?

🐛 Bug report
Status

Closed: won't fix

Version

2.0

Component

Code

Created by

🇩🇪Germany Umac_de

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

Comments & Activities

  • Issue created by @Umac_de
  • Assigned to Rajan Kumar@2026
  • Issue was unassigned.
  • 🇩🇪Germany Umac_de

    This code at line 28ff does the job for me:

    if($this->getAmount() != NULL){
          $amount = $this->getAmount();
          if ($amount->getCurrencyCode() != $shipment->getAmount()->getCurrencyCode()) {
            return;
          }
        }else{
          $amount = $shipment->getAmount();
        };
  • Status changed to Needs review 6 months ago
  • Hi @Umac_de,
    I have added the code as requested. Please refer to the attached patch.
    Thank you.

  • Status changed to Closed: won't fix 6 months ago
  • 🇮🇱Israel jsacksick

    This bug is weird...
    The parent class already calls appliesToShipment() prior to calling the applyToShipment() method.

    protected function appliesToShipment(ShipmentInterface $shipment) {
        if (!$shipment->getShippingMethodId() || !$shipment->getAmount()) {
          // The shipment is still incomplete, skip it.
          return FALSE;
        }

    And it returns false if !$shipment->getAmount() is NULL.

    So based on comment #4 and the backtrace, the problem seems to be due to the amount that isn't set on the promotion offer. That is not possible from the UI as the field is required which seems to indicate your promotion is malformed, so this isn't something that should be solved with a patch.

    Are you creating promotions programatically? If that is the case, you should revise the code to properly set the amount offer. Closing this as won't fix.

  • 🇮🇱Israel jsacksick

    Attaching a screenshot of the admin UI so you can see that the promotion can't be saved if the amount isn't filled:

    So once again a data problem to me.

  • 🇩🇪Germany Umac_de

    @ jsacksick

    No, I am not creating promotions programatically, but, you are right, the amount is missing on the config site.

    Don't know how that happened.
    Thank you!

  • 🇮🇱Israel jsacksick

    @Umac_de: weird... I believe this is a better fix, as fixing the promotion offer code just turns this bug into a silent one... Better to fix a malformed promotion that probably shouldn't be active anymore than having it silently fail IMO.

Production build 0.71.5 2024