How to change Add to Cart button text and link in WooCommerce without plugin

Today you will know how you can change the Add To Cart button text and link in woocommerce, without installing any plugin. Sometimes we want to use Add to cart button for different purposes in that case we need to add some custom code in our active theme functions.php file. 

WooCommerce provide us many hooks using that we can do the modifications without modifying the plugin core files. 


To change add to cart button text on single product page :

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_product_single_add_to_cart_text_custom' ); 

function woocommerce_product_single_add_to_cart_text_custom() {

    return __( 'Buy Now', 'woocommerce' ); 

}


To change add to cart button text on product archives pages:

add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_product_add_to_cart_text_custom' );  

function woocommerce_product_add_to_cart_text_custom() {

    return __( 'Buy Now', 'woocommerce' );

}


To change add to cart button text and link on product archives pages:

- Using the following code in your theme functions.php file you can change the button text and url in product archive pages.

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button_text_link', 10, 2 );

function replacing_add_to_cart_button_text_link( $button, $product  ) {

    $button_text = __("More Info", "woocommerce");

    $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';

    return $button;

}


If you don't want to change external product type products button link and text then you need to add one condition in this code like this:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button_text_link', 8, 2 );

function replacing_add_to_cart_button_text_link( $button, $product  ) {

    if( !$product->is_type( 'external' ) ) {

    $button_text = __("More Info", "woocommerce");

    $button = '<a class="button product_type_simple add_to_cart_button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';

    }

    return $button;

}


Output: 


Add to Cart Button Changes



Post a Comment

0 Comments