api - 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;
}
解决方案
你可以参考这个扩展。我发现它在您的情况下非常有用https://marketplace.magento.com/sanjay-module-custom-currency-updater.html
推荐阅读
- apache-spark - 使用 spark-defaults.conf 文件在 jupyter notebook 中初始化 pyspark
- c# - 将视图绑定到视图模型并在 Xamarin.Forms 中显示为对话框页面?
- html - 右导航栏向下滚动
- javascript - 导出 PapaParse 函数的问题
- html - 从布局组件对齐 Antd 的右键菜单
- bash - IBM Cloud CLI 的 Bash 自动完成?
- java - 将 SceneForm/ARCore 添加到 Gradle 时 Android App 无法编译
- java - 无法从 eclipse 连接到 oracle db
- python - 涉及前一行数据的 Python 矢量化操作
- npm - 我可以在*同时*从 CLI 安装依赖项和 devDependencies 吗?