user2679140 user2679140 - 3 months ago 30
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;

http://docs.woothemes.com/document/change-add-to-cart-button-text/

Thanks.

Answer

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' );
        endif;
    }
    //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' );
        else:
            return __( 'Add to Cart', 'woocommerce' );
        endif;
    }  

Let me know the output.

Comments