如何修复WooCommerce外部产品按钮文字修改无效问题?
解决WooCommerce外部产品按钮文字修改无效问题
你之前使用的woocommerce_product_add_to_cart_text过滤器无法生效,核心原因是WooCommerce的外部/联盟产品有专属的钩子逻辑,且代码未针对产品类型做精准判断,导致无法触发修改。
正确实现代码
以下代码分别针对单个产品页面和产品列表页的外部产品按钮修改文字,直接添加到当前主题的functions.php文件末尾即可:
// 修改单个外部产品页面的按钮文字 add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_external_single_button_text', 10, 2 ); function custom_external_single_button_text( $button_text, $product ) { if ( 'external' === $product->get_type() ) { $button_text = 'Bekijk bij BOL.COM ➞'; } return $button_text; } // 修改产品列表页的外部产品按钮文字 add_filter( 'woocommerce_product_add_to_cart_text', 'custom_external_archive_button_text', 10, 2 ); function custom_external_archive_button_text( $button_text, $product ) { if ( 'external' === $product->get_type() ) { $button_text = 'Bekijk bij BOL.COM ➞'; } return $button_text; }
额外排查点
如果代码仍不生效,检查以下内容:
- 主题是否重写了外部产品模板:查看主题目录下是否有
single-product/add-to-cart/external.php文件。若存在,确认文件中是否通过apply_filters调用了对应钩子;如果是硬编码固定文字,直接修改模板内的$button_text变量即可。 - 清除网站缓存和浏览器缓存,确保修改后的代码被正常加载。
- 优先使用代码片段插件添加代码,避免主题更新时覆盖
functions.php内容。
内容的提问来源于stack exchange,提问作者Thijs van Hal
相关产品推荐
相关产品推荐

