user2679140 user2679140 - 1 year ago 156
PHP Question

Change WooCommerce add to cart text for specific tag

I am looking for a function to change the Add to Cart text on a woocommerce button but only if the product in question has a specific tag. ie if the product has the tag "preorder" the button text changes to "Pre Order Now"

Changing the text globally can be achieved with this;


Answer Source

You can check for that particular term with has_term.

    //For single product page
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
    function woo_custom_cart_button_text() {
        global $product;
        if ( has_term( 'preorder', 'product_cat', $product->ID ) ) :
            return __( 'Pre order Now!', 'woocommerce' );
    //For Archive page
    add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
    function woo_archive_custom_cart_button_text() {
        if ( has_term( 'preorder', 'product_cat', $product->ID ) ) :
            return __( 'Pre order Now!', 'woocommerce' );
            return __( 'Add to Cart', 'woocommerce' );

Let me know the output.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download