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!

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
    – Marius
    Feb 2 at 8:11
  • 1
    @Marius please correct the typo: $order->getExcentionAttributes() must be $order-> getExtensionAttributes() (someone can copy this) Feb 2 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.

1
  • Works like a charm,thanks Feb 6 at 7:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.