使用 wc_price 过滤器挂钩向产品价格添加其他货币

2024-02-06

根据我原来帖子的答案使用时遇到格式不正确的数值wc_priceWooCommerce 挂钩 https://stackoverflow.com/questions/66084833/a-non-well-formed-numeric-value-encountered-while-using-wc-price-woocommerce-hoo,我现在尝试向该函数添加其他货币,最后将它们全部输出。

我决定使用四列的 DIV 部分,其中 CSS 使其具有响应能力。

.exchanged-price{
float: left;
width: 25%;
padding: 15px;
}

.exchange-rate-wrapper:after{
content: "";
display: table;
clear: both;
}

@media screen and (max-width: 900px){
.exchanged-price{
width: 50%;
}}

@media screen and (max-width: 600px){
.exchanged-price{
width: 100%;
}}

然后我删除了该功能的使用,认为我不需要它。

这是新的代码,这给了我以下错误:

Parse error: syntax error, unexpected 'return' (T_RETURN)

这是新代码:

add_filter( 'wc_price', 'filter_wc_price', 10, 5 );
function filter_wc_price( $return, $price, $args, $unformatted_price, $original_price = null ) {

    // EUR
    $conversion_rate_eur = (float) 1.25;
    $symbol_eur = 'EUR';
    $currency_symbol_eur = get_woocommerce_currency_symbol( $symbol_eur );
    $euro_price = (float) $price * $conversion_rate_eur;
    // return number_format( $euro_price, 2, '.', '' );

    // US dollar
    $conversion_rate_us = (float) 0.85;
    $symbol_us = 'US';
    $currency_symbol_us = get_woocommerce_currency_symbol( $symbol_us );
    $us_price = (float) $price * $conversion_rate_us;
    // return number_format( $us_price, 2, '.', '' );

    // GP brittish pound
    $conversion_rate_gbp = (float) 1.35;
    $symbol_gbp = 'GBP';
    $currency_symbol_gbp = get_woocommerce_currency_symbol( $symbol_gbp );
    $us_price = (float) $price * $conversion_rate_us;
    // return number_format( $us_price, 2, '.', '' );


    $exchange_rate_section = '
    <div class="exchange-rate-wrapper">
        
        <div class="exchanged-price">
        <h2>' return number_format( $euro_price, 2, '.', '' ); '</h2>
        </div>

        <div class="exchanged-price">
        <h2>'return number_format( $us_price, 2, '.', '' ); '</h2>
        </div>

        <div class="exchanged-price">
        <h2>'return number_format( $gbp_price, 2, '.', '' ); '</h2>
        </div>

        <div class="exchanged-price">
        <h2></h2>
        </div>

    </div>';

    return $return . '<br>' . $exchange_rate_section;
}

有一些错误:

  • 您正在将 PHP 代码与其他字符串一起编写,而不使用字符串连接符号. (PHP:字符串运算符 https://www.php.net/manual/en/language.operators.string.php).

  • The $gbp_price变量尚未初始化和/或赋值。

  • 获取美元货币的符号是USD并不是US.

所以正确的函数是:

add_filter( 'wc_price', 'filter_wc_price', 10, 5 );
function filter_wc_price( $return, $price, $args, $unformatted_price, $original_price = null ) {

    // EUR
    $conversion_rate_eur = (float) 1.25;
    $symbol_eur = 'EUR';
    $currency_symbol_eur = get_woocommerce_currency_symbol( $symbol_eur );
    $euro_price = (float) $price * $conversion_rate_eur;
    // return number_format( $euro_price, 2, '.', '' );

    // US dollar
    $conversion_rate_us = (float) 0.85;
    $symbol_us = 'USD';
    $currency_symbol_us = get_woocommerce_currency_symbol( $symbol_us );
    $us_price = (float) $price * $conversion_rate_us;
    // return number_format( $us_price, 2, '.', '' );

    // GP brittish pound
    $conversion_rate_gbp = (float) 1.35;
    $symbol_gbp = 'GBP';
    $currency_symbol_gbp = get_woocommerce_currency_symbol( $symbol_gbp );
    $gbp_price = (float) $price * $conversion_rate_us;
    // return number_format( $gbp_price, 2, '.', '' );


    $exchange_rate_section = '
    <div class="exchange-rate-wrapper">
        
        <div class="exchanged-price">
        <h2>' . number_format( $euro_price, 2, '.', '' ) . ' ' . $currency_symbol_eur . '</h2>
        </div>

        <div class="exchanged-price">
        <h2>'. number_format( $us_price, 2, '.', '' ) . ' ' . $currency_symbol_us . '</h2>
        </div>

        <div class="exchanged-price">
        <h2>' . number_format( $gbp_price, 2, '.', '' ) . ' ' . $currency_symbol_gbp . '</h2>
        </div>

        <div class="exchanged-price">
        <h2></h2>
        </div>

    </div>';

    return $return . '<br>' . $exchange_rate_section;
}

该函数已经过测试,不会返回错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 wc_price 过滤器挂钩向产品价格添加其他货币 的相关文章

随机推荐

  • 由于 Windows Vista Home Premium 版本缺少 Windows 身份验证 IIS 组件...有修复吗?

    显然 Windows Vista Home Basic 和 Premium 都没有附带 IIS 的集成 Windows 身份验证 您需要商业版 所以有人知道如何为 IIS 安装 Windows 身份验证吗 我进入程序和功能 gt 打开或关闭
  • 如何使用脆皮形式格式化 django-filters?

    是否可以使用 django crispy forms 格式化 django filters 过滤器表单 我一直在尝试这样做 但是django filters FilterSet似乎不接受脆脆的表单格式 来自 DeviceFilter 类 它
  • jol GraphLayout 输出中的“(其他内容)”是什么?

    当使用 jol 的 GraphLayout 类打印从对象实例引用的对象图时 某些输出条目显示 其他内容 而不是类型和引用路径 例如 考虑以下代码 该代码打印 20 个随机 Integer 对象列表的图形 List
  • 如何与我的适配器类中的片段进行通信

    我创建了一个自定义适配器类 在该类中 当我单击列表视图布局时 我有一个代码必须向我的片段发送消息 谷歌搜索后 最好的方法可能是使用界面 其中大部分都是activity与fragment之间进行通信的例子 但就我而言 我对如何在适配器类与片段
  • 将 C Struct 编组为 C# 委托的返回值

    我试图从绑定到本机函数的委托中按值返回一个小 8 字节 结构 但在针对 NET Framework 2 0 时遇到以下错误 在针对 4 0 时 代码似乎可以正常工作 testclient exe 中发生类型为 System AccessVi
  • 传递格式化的 NSDate

    我传递给实例方法的格式化日期未保存 打印正确的日期 有人可以帮忙吗 调用方法代码片段 NSDateFormatter dateFormatter NSDateFormatter alloc init autorelease dateForm
  • 卡在 PHP 查询上

    我创建了一个页面 允许用户更改密码和电子邮件 所有这些都有效 但由于某种原因 当我只想更改我的电子邮件时 我也得到了该字段当前密码不正确 电子邮件本身在数据库中发生了变化 但这显示了出来 我显然已经验证了它的显示 但我不确定如何编写一个新查
  • 如何让它更短(Pythonic)?

    我必须检查很多世界是否在字符串中 代码如下所示 if string 1 in var string or string 2 in var string or string 3 in var string or string n in var
  • 具有本机扩展的 Ruby gem 无法在 AWS Lambda 上运行

    我有一个 ruby 脚本 正在尝试在 AWS Lambda 上运行 我如何让它使用带有本机扩展的 Ruby gem 我已经通过安装了我的 Ruby gemsbundle install deployment并将它们包含在我的部署中 当我在
  • C# 更改系统区域设置

    需要将系统区域设置更改为不同的国家 地区 我尝试过 SystemParametersInfo GetKeyboardLayout 但没有帮助 如何更改控制台应用程序的 C 系统区域设置 e g Thread CurrentThread Cu
  • 更改大表中的mysql字段名称

    我有一个包含 2100 万行的表 我必须更改其中一个行名称 当我尝试使用查询 alter table company change id new id int 11 时 查询永远不会结束 有没有简单的方法来更改大mysql表字段名称 首先
  • Excel:查找数组中的最后一个值

    我有一个数据集 gt A b c d AA BB gt 1 2 3 4 gt apple apple apple gt orange pear pear apple pear gt grapefruit grape grape grape
  • Eclipse 在 Ubuntu 14.04 中无法启动

    我尝试在 Ubuntu 14 04 中启动 Eclipse 时遇到问题 启动图片弹出 然后闪烁 变成白色 直到王国来临之前什么也没有发生 我尝试过 Luna Kepler 和 3 8 来自 Ubuntu 存储库 EE 和 SE 版本 两者相
  • Service Worker 可以缓存 POST 请求吗?

    我尝试在 fetch 事件的服务工作人员中缓存 POST 请求 I used cache put event request response 但返回的承诺被拒绝TypeError Invalid request method POST 当
  • 如何在打印语句中使用零填充标志精确打印两个位置

    如果想使用零垫进行此方法打印 你该怎么做 int month day public void printNumeric System out printf month day n i would like the month if it i
  • Tkinter ttk 查看自定义主题设置

    使用后ttk Style theme create name settings 可以看到该主题的设置吗 我问的原因是当我创建一个新主题并添加ttk Notebook root 对于我的代码 选项卡有圆角 这是我不想要的 这是一个例子 imp
  • 通过引用传递给构造函数

    我决定看看为成员分配引用是否会使成员成为引用 我编写了以下代码片段来测试它 有一个简单的类Wrapper与std string作为成员变量 我采取采取const string 在构造函数中并将其分配给公共成员变量 后来在main 方法我修改
  • Sublime Text 3 Windows 使用 Alt 选择列?

    Shift right click feels unintuitive to me How can I tell ST3 to allow Alt drag to do column selection like in many other
  • 为什么要重写 DTO 中的 toString 方法

    我看到大多数时候在DTO对象中 toString 方法实际上被重写了 例如 public class Person implements Serializable private String firstName private Strin
  • 使用 wc_price 过滤器挂钩向产品价格添加其他货币

    根据我原来帖子的答案使用时遇到格式不正确的数值wc priceWooCommerce 挂钩 https stackoverflow com questions 66084833 a non well formed numeric value