You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPML如何适配自定义修改的WooCommerce加购提示消息?

解决方案

你之前的代码中自定义的提示文本是硬编码内容,未使用WordPress本地化函数包裹,因此WPML无法识别该字符串,修改WooCommerce原生提示自然不会生效,可按照以下步骤解决:

  • 替换原有加购提示过滤代码,修改后代码如下:
add_filter( 'wc_add_to_cart_message_html', function ( $message ) {
    // 使用__()函数包裹自定义文本,指定自定义文本域供WPML扫描识别
    $text = __( 'Product added to your cart.', 'custom-woocommerce-text' );
    return sprintf(
        '<a href="%s" tabindex="1" class="button wc-forward">%s</a> %s',
        esc_url( wc_get_cart_url() ),
        esc_html__( 'View cart', 'woocommerce' ),
        esc_html( $text )
    );
} );
  • 进入WPML管理后台的「字符串翻译」页面,点击顶部「扫描待翻译字符串」按钮,选择你自定义的文本域custom-woocommerce-text执行扫描。
  • 扫描完成后即可在字符串翻译列表中找到Product added to your cart.条目,添加对应语言的翻译并保存,前台即可生效。

注:代码里的View cart调用的是WooCommerce原生本地化字符串,只要你已在WPML中翻译过WooCommerce对应文本,按钮会自动适配多语言,无需额外修改。

内容的提问来源于stack exchange,提问作者StEst

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 06:51:08