wordpress - 将小计添加到结帐按钮
问题描述
我正在尝试通过以下代码将小计添加到结帐按钮
add_filter( 'gettext', function( $translated_text ) {
if ( 'Checkout' === $translated_text ) {
$subtotal = WC()->cart->get_subtotal();
$translated_text = "EXPRESS " . $translated_text . " -$" . $subtotal;
}
return $translated_text;
} );
但是,我在日志中看到以下错误 Got error 'PHP message: PHP Fatal error: Uncaught Error: Call to a member function get_subtotal() on null in ....file name.....
解决方案
需要添加:
if ( ! isset( WC()->cart ) ) return;
完整代码:
add_filter( 'gettext', function( $translated_text ) {
if ( 'Checkout' === $translated_text ) {
if ( ! isset( WC()->cart ) ) return;
$subtotal = WC()->cart->get_subtotal();
$translated_text = "EXPRESS " . $translated_text . " - $" . $subtotal;
}
return $translated_text;
} );
推荐阅读
- expect - 循环在除了脚本变量从文本文件中获取值
- arrays - 如何遍历字典内的多个嵌套列表?
- python - 怪异的Python堆:heappop()第一次输出
- r - R导入音频文件以计算音频多样性时出错
- c++ - 可变参数模板构造函数填充 std::variant 的内部向量
- sql - 如何在 Hive 或 Presto 中将以下字典格式列转换为不同的格式?
- php - 如何在 Laravel/Eloquent 中查询嵌套的不同记录?
- reactjs - 为什么 useEffect 的依赖数组中需要历史记录
- r - 需要帮助制作 Ordihull
- javascript - 单击按钮时在 javascript 中执行 selenium 脚本