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

如何在Woocommerce中针对特定分类修改归档页加入购物车文本

Solution for Customizing "Add to Cart" Text on Preorder Category Archive Pages

Got it, let's tweak your code so the "Add to Cart" text only changes on the preorder product category archive page. Your original code replaces the text site-wide, so we just need to add a targeted conditional check to fix that.

Updated Code (Targeting Preorder Category Archive Only)

// Use both filter hooks for compatibility with old and new WooCommerce versions
add_filter( 'add_to_cart_text', 'woo_archive_preorder_cart_button_text' ); // For WooCommerce < 2.1
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_preorder_cart_button_text' ); // For WooCommerce >= 2.1

function woo_archive_preorder_cart_button_text( $default_text ) {
    // Check if we're on the 'preorder' product category archive page
    // Note: 'preorder' here must match your category's slug, not its display name
    if ( is_product_category( 'preorder' ) ) {
        return __( 'Preorder', 'woocommerce' );
    }

    // Return the original text for all other pages/contexts
    return $default_text;
}

How This Works:

  • Compatibility: We include both filter hooks to cover older and newer WooCommerce releases, so this code works no matter which version you're using.
  • Targeted Check: The is_product_category('preorder') function verifies if the current page is the archive for your preorder product category. Double-check that your category's slug matches this value (you can find the slug in WooCommerce > Products > Categories > Edit your preorder category).
  • Non-Disruptive Fallback: If we're not on the preorder category archive, we return the default "Add to Cart" text, so no other pages are affected.

Bonus: Target All Preorder Products (Any Page They Appear On)

If you want the "Preorder" text to show up for any product in the preorder category—whether it's on the category archive, search results, or homepage—use this code instead:

add_filter( 'woocommerce_product_add_to_cart_text', 'woo_preorder_product_cart_button_text' );

function woo_preorder_product_cart_button_text( $default_text ) {
    global $product;

    // Safety check: skip if no product object exists
    if ( ! $product ) {
        return $default_text;
    }

    // Check if the product belongs to the 'preorder' category
    if ( has_term( 'preorder', 'product_cat', $product->get_id() ) ) {
        return __( 'Preorder', 'woocommerce' );
    }

    return $default_text;
}

Just drop whichever code fits your needs into your theme's functions.php file or a custom plugin.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:44:46