首页 > 解决方案 > Magento 2.4.2:如何在我的自定义模块中获取实时当前汇率?

问题描述

到目前为止,在 Magento 应用程序/代码中,一个自定义模块一直在处理美国订单的运输。由于我们计划将订单运送到加拿大,因此现在需要获取实时汇率以帮助我以加元重新计算小计(美国的销售价格)。下面是我需要获取美元和加元之间汇率的代码部分。有没有办法做到这一点?

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;
}

标签: apilogicmagento2currency

解决方案


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


推荐阅读