0

Magento 2.4.6

I am trying to get an orders surcharge amount which was added by the Fooman Surcharge extension.

In Magento 1 it worked with $order->getFoomanSurchargeAmount();, in Magento 2 it doesnt work this way.

Any ideas how to get the amount from an order object?

Thanks!

Deepak MageDivine
4931 gold badge5 silver badges23 bronze badges
asked Feb 2, 2024 at 7:57
2
  • I;m not familiar with the extension, But I know "Fooman" and I know he does things the right way, so there is a high chance he added an extension attribute for the sircharge. Try it with $order->getExtensionAttributes()->getFoomanSurchargeAmount(). If that does not work try logging somewhere $order->getExtensionAttributes() and see how it looks like. maybe the attribute name is different Commented Feb 2, 2024 at 8:11
  • 1
    @Marius please correct the typo: $order->getExcentionAttributes() must be $order-> getExtensionAttributes() (someone can copy this) Commented Feb 2, 2024 at 12:27

1 Answer 1

1

The surcharges added by our extension are available via extension attributes. As there can be multiple different surcharges there is a top level totals group item. Please see below for an example to get to the surcharges including cases where no surcharges are present:

private function processFoomanTotals(\Magento\Sales\Api\Data\OrderInterface $order)
{
 $extAttr = $order->getExtensionAttributes();
 if (!$extAttr) {
 return;
 }
 $foomanGroup = $extAttr->getFoomanTotalGroup();
 if (empty($foomanGroup)) {
 return;
 }
 $totals = $foomanGroup->getItems();
 if (empty($totals)) {
 return;
 }
 foreach ($totals as $total) {
 /** @var \Fooman\Totals\Api\Data\OrderTotalInterface $total */
 $description = $total->getLabel();
 $baseAmount = $total->getBaseAmount();
 $baseTaxAmount = $total->getBaseTaxAmount();
 $amount = $total->getAmount();
 $taxAmount = $total->getTaxAmount();
 $identifier = $total->getTypeId();
 $qty = 1;
 /** @TODO do something with the total */
 }
}

Or as an alternative you could use Fooman\Totals\Model\OrderTotalManagement::getByCodeAndOrderId($code, $orderId) where $code is fooman_surcharge.

answered Feb 5, 2024 at 2:14
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.