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

修改WooCommerce店铺及分类页加入购物车按钮点击文本

解决WooCommerce多页面添加购物车按钮文本修改问题

原代码仅针对单个产品页生效,要让店铺页和产品分类页也生效,需要调整两个核心点:取消页面限制、适配多页面按钮选择器并使用WooCommerce官方事件监听(避免AJAX场景下文本被覆盖)。

修改后的代码如下:

add_action( 'wp_footer', 'wc_add_to_cart_button_loading_text' );
function wc_add_to_cart_button_loading_text() {
    global $product;

    // 排除可变产品(不需要排除可删除此行)
    if ( isset( $product ) && $product->is_type('variable') ) return;
    ?>
    <script type="text/javascript">
        (function($){
            // 监听WooCommerce添加购物车开始事件
            $(document).on('adding_to_cart', function(event, btn) {
                // 保存原始按钮文本
                $(btn).data('original-text', $(btn).text());
                // 修改为加载状态文本
                $(btn).text('<?php _e( "Adding...", "woocommerce" ); ?>');
                // 禁用按钮防止重复点击
                $(btn).prop('disabled', true);
            });

            // 监听添加购物车完成事件,恢复按钮原始文本(可选)
            $(document).on('added_to_cart', function(event, fragments, cart_hash, btn) {
                $(btn).text($(btn).data('original-text'));
                $(btn).prop('disabled', false);
            });
        })(jQuery);
    </script>
    <?php
}

代码说明:

  • 移除了is_product()判断,代码会在所有页面加载(包括店铺页、分类页、单个产品页)
  • 使用WooCommerce官方的adding_to_cart和added_to_cart事件,比单纯监听click更可靠,适配AJAX添加购物车的场景
  • 保留了排除可变产品的逻辑,不需要的话可直接删除对应判断行
  • 增加按钮禁用/启用逻辑,避免用户重复点击
  • 可选的added_to_cart事件监听,用于添加完成后恢复按钮原始文本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 08:05:30