如何为指定产品修改Woocommerce Extra Product Option插件的Final total文本
修正代码:针对指定产品修改Woocommerce Extra Product Option的“Final total”文本
原代码的问题
- 文本域错误:插件的官方文本域是
woocommerce-extra-product-options,你写的'Final total text'不符合插件实际配置 - 缺少产品限定:没有添加判断逻辑,无法只在ID为9477的产品页面生效
- 钩子不够精准:使用全局
gettext钩子容易干扰其他文本,建议用插件专属钩子
正确代码实现
function change_epo_final_total_text( $translated_text, $text, $domain ) { // 限定仅在ID为9477的产品页面生效 global $product; if ( ! is_a( $product, 'WC_Product' ) || $product->get_id() !== 9477 ) { return $translated_text; } // 匹配原文本与插件正确文本域 if ( $text === 'Final total' && $domain === 'woocommerce-extra-product-options' ) { $translated_text = '自定义最终总价'; // 替换为你需要的文本 } return $translated_text; } // 用插件专属钩子,优先级20确保覆盖默认翻译 add_filter( 'gettext_woocommerce-extra-product-options', 'change_epo_final_total_text', 20, 3 );
注意事项
- 替换代码中的
'自定义最终总价'为你实际需要显示的内容 - 代码可添加到当前主题的
functions.php文件,或使用Code Snippets这类自定义代码插件 - 确认产品ID为9477:可在产品编辑页面的URL中查看(如
wp-admin/post.php?post=9477&action=edit)
内容的提问来源于stack exchange,提问作者MohsenJames Amra
相关产品推荐
相关产品推荐

