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!
1 Answer 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.
$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$order->getExcentionAttributes()must be$order-> getExtensionAttributes()(someone can copy this)