Magento 2.4.2:如何在我的自定义模块中获取实时当前汇率?

Magento 2.4.2: How to get real time current exchange rate in my custom module?

在 Magento app/code 中,自定义模块一直在处理美国订单的运输。由于我们计划将订单运送到加拿大,现在需要获得实时汇率以帮助我重新计算加元的小计(美国售价)。下面是我需要获取美元和加元之间汇率的代码部分。有办法实现吗?

if ($request->getDestCountryId() == "CA") {
    $this->_logger->info("Country ID: " . $request->getDestCountryId());
    $fedex_results = $this->carrierFedex->collectRates($request);
    // $this->_logger->info("Fedex Rate " . $fedex_rate);
    // 1. Get the array of method objects from $fedex_results object
    // 2. Loop through the array of method objects to set carrier.
    // 3. Set carrier to custom shipping. 
    // 4. Add brokerage to the shipping cost based on the table canada_brokerage_fees.

    // \Magento\Quote\Model\Quote\Address\RateResult\Method $methods
    $methods = $fedex_results->getAllRates();
    foreach ($methods as $method) {
        $method->setCarrier($this->_code);
        // Set new price with brokerage added to the shipping cost.
        // Get subtotal from the $request object.
        // Recalculate subtotal in CAD
        // Create table canada_brokerage_fee in Magento DB
        // Compute brokerage.
        $subtotal = $request->getOrderSubtotal();
        //recalculate $subtotal in CAD.
        **// Code to get the exchange rate...**

        $brokerage = 0;
        //$method->setPrice($method->getPrice+$brokerage);
    }

    return $fedex_results;
}

您可以参考这个扩展。我发现它对你的情况很有用 https://marketplace.magento.com/sanjay-module-custom-currency-updater.html