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

如何在WooCommerce(Bricks Builder)移除AJAX加购按钮的‘added’与‘view cart’?

WooCommerce + Bricks Builder:隐藏AJAX加购后的状态提示与「查看购物车」按钮

一、CSS精准修复方案(保留原按钮)

你之前的CSS会把整个按钮移除,因为.added类是WooCommerce添加在原加入购物车按钮容器/自身上的,直接隐藏会覆盖原按钮元素。以下是精准定位目标元素的修复代码:

/* 隐藏「查看购物车」链接 */
.added .added_to_cart.wc-forward {
    display: none !important;
}

/* 隐藏加购后的状态文本(如「已添加」),恢复原按钮文本 */
.added .add_to_cart_button-text {
    display: none !important;
}
.added .add_to_cart_button:before {
    content: "加入购物车" !important;
}

如果Bricks Builder的按钮结构有差异(比如状态文本放在独立<span>标签里),可以用更适配的选择器:

/* 适配Bricks的按钮结构 */
.added .add_to_cart_button .added-text {
    display: none;
}
.added .add_to_cart_button .default-text {
    display: inline-block !important;
}

二、PHP钩子方案(彻底移除提示元素)

如果不想让这些提示元素出现在DOM中,可通过WooCommerce钩子从根源处理,代码可添加到主题functions.php或Bricks的自定义代码区域(Bricks → 设置 → 自定义代码):

// 移除「查看购物车」跳转链接
add_filter( 'woocommerce_add_to_cart_redirect', '__return_false' );

// 清空加购成功的提示消息
add_filter( 'wc_add_to_cart_message_html', '__return_empty_string' );

// 加购后移除按钮的added类,恢复初始状态
add_action( 'wp_footer', function() {
    ?>
    <script>
        jQuery(document).on('added_to_cart', () => {
            jQuery('.add_to_cart_button').removeClass('added');
        });
    </script>
    <?php
});

三、无需默认类实现AJAX加购

如果要给不带.ajax_add_to_cart.add_to_cart_button类的按钮实现AJAX加购,可通过自定义JS触发:

jQuery(document).ready(function($) {
    // 绑定自定义按钮的AJAX加购事件
    $('.custom-add-to-cart-btn').on('click', function(e) {
        e.preventDefault();
        const productId = $(this).data('product_id'); // 按钮需携带data-product_id属性
        
        $.ajax({
            url: wc_add_to_cart_params.ajax_url,
            type: 'POST',
            data: {
                action: 'woocommerce_add_to_cart',
                product_id: productId,
                quantity: 1 // 可按需调整购买数量
            },
            success: function(res) {
                // 触发购物车更新事件(同步购物车计数等)
                $(document.body).trigger('added_to_cart', [res.fragments, res.cart_hash]);
            }
        });
    });
});

给你的自定义按钮添加custom-add-to-cart-btn类和data-product_id="对应产品ID"属性即可生效。

内容的提问来源于stack exchange,提问作者Théo Florès

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 12:35:24