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

当商品已在购物车时修改Add To Cart按钮的文本与链接

Customize Add To Cart Button on WooCommerce Archive Pages When Product Is In Cart

Hey there! Let's fix this button behavior for your WooCommerce archive pages—since you've got a 1-item limit per product, we can tweak the button once the product's already in the cart with two straightforward solutions.

Solution 1: Show "View Cart" Button Linking to Cart Page

This swaps the button text to "View Cart" and sends users directly to the cart page when the product’s already in their cart. Add this code to your theme’s functions.php file (or use a custom plugin to keep changes safe during theme updates):

// Update Add To Cart text to "View Cart" for products in cart
add_filter('woocommerce_product_add_to_cart_text', 'custom_cart_button_text_view_cart', 10, 2);
function custom_cart_button_text_view_cart($text, $product) {
    // Check if product is in cart and has 1-item purchase limit
    if (WC()->cart->find_product_in_cart(WC()->cart->generate_cart_id($product)) && $product->get_max_purchase_quantity() == 1) {
        $text = __('View Cart', 'woocommerce');
    }
    return $text;
}

// Redirect button to cart page for products already in cart
add_filter('woocommerce_loop_add_to_cart_link', 'custom_cart_button_link_view_cart', 10, 2);
function custom_cart_button_link_view_cart($link, $product) {
    if (WC()->cart->find_product_in_cart(WC()->cart->generate_cart_id($product)) && $product->get_max_purchase_quantity() == 1) {
        $link = sprintf(
            '<a href="%s" class="button %s">%s</a>',
            esc_url(wc_get_cart_url()),
            esc_attr('add_to_cart_button'),
            esc_html(__('View Cart', 'woocommerce'))
        );
    }
    return $link;
}

This changes the button text to "Added" and removes the clickable link entirely (so clicking does nothing). Use this code instead:

// Update Add To Cart text to "Added" for products in cart
add_filter('woocommerce_product_add_to_cart_text', 'custom_cart_button_text_added', 10, 2);
function custom_cart_button_text_added($text, $product) {
    if (WC()->cart->find_product_in_cart(WC()->cart->generate_cart_id($product)) && $product->get_max_purchase_quantity() == 1) {
        $text = __('Added', 'woocommerce');
    }
    return $text;
}

// Remove link from button for products already in cart
add_filter('woocommerce_loop_add_to_cart_link', 'custom_cart_button_remove_link', 10, 2);
function custom_cart_button_remove_link($link, $product) {
    if (WC()->cart->find_product_in_cart(WC()->cart->generate_cart_id($product)) && $product->get_max_purchase_quantity() == 1) {
        $link = sprintf(
            '<span class="button %s">%s</span>',
            esc_attr('add_to_cart_button'),
            esc_html(__('Added', 'woocommerce'))
        );
    }
    return $link;
}

Quick Notes:

  • Always backup your theme files before adding code, or use a child theme to avoid losing changes when your parent theme updates.
  • The $product->get_max_purchase_quantity() == 1 check ensures this only applies to your 1-item limit products. If you want this behavior for all products, just remove that part of the condition.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:50:36