I want to change the cart button text on archive pages to an icon.
I found a snippet which does that. But that also changes the link of the cart button:
add_filter( 'woocommerce_loop_add_to_cart_link', 'ts_replace_add_to_cart_button', 10, 2 ); function ts_replace_add_to_cart_button( $button, $product ) { if (is_product_category() || is_shop()) { $button_text = __("View Product", "woocommerce"); $button_link = $product->get_permalink(); $button = '<a href="' . $button_link . '">' . $button_text . '</a>'; return $button; } }
Is there a way to change only the text of the button?
I know, that I could change the template file /loop/add-to-cart.php
. But I need a solution based on a function.
Advertisement
Answer
That should do the trick. You can adjust the conditional statement. Right now it’s covering any taxonomy.php
page, which are an archive page for the products (Woocommerce), and the regular archive.php
page.
<?php /** * do_action( 'wp_customize_add_to_cart_archive' ) * This hook is fired once WP, all plugins, and the theme are fully loaded and instantiated. * @link https://developer.wordpress.org/reference/hooks/wp_loaded/ */ add_action( 'wp_customize_add_to_cart_archive', function() { function wp_customize_add_to_cart_archive( $subject ) { if( ! is_admin() && is_archive() || is_tax() ) { $search = [ '/>Add to cart</', // ... >< with the brackets, to be sure to target the right Home word // ... etc. ]; $replace = [ '><i class="fas fa-play-circle"></i><', // ... Our replacement for Add to cart using a font awesome icon // ... etc. ]; $subject = preg_replace( $search, $replace, $subject ); return $subject; }; }; ob_start( 'wp_customize_add_to_cart_archive' ); } ); ?>
Learn more
preg_replace
@ https://www.php.net/manual/en/function.preg-replace.phpob_start
@ https://www.php.net/manual/en/function.ob-start.php