Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeveloperWil/7a88086d8eea1be94dfe4f391b5201a1 to your computer and use it in GitHub Desktop.
Save DeveloperWil/7a88086d8eea1be94dfe4f391b5201a1 to your computer and use it in GitHub Desktop.
WooCommerce: Change Add To Cart Button Text and URL
/**
* Removes the default Add To Cart button from the WooCommerce loop
* This will affect all products site-wide
*
* @author Wil Brown zeropointdevelopment.com
*/
function zpd_remove_wc_loop_add_to_cart_button(){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('init','zpd_remove_wc_loop_add_to_cart_button');
/**
* Replaces the Add To Cart button with a new button with custom text and a custom URL link
* This will affect all products site-wide
* You can change the style of the button by using CSS on p.zpd-wc-reserve-item-button{}
*
* @author Wil Brown zeropointdevelopment.com
*/
function zpd_replace_wc_add_to_cart_button() {
global $product;
// This adds some URL query variables that may be useful to input into a contact form - remove if not needed
$product_link_params = sprintf( '?wc_id=%s&wc_price=%s&wc_title=%s&wc_product_link=%s',
$product->get_id(),
$product->get_display_price(),
$product->get_title(),
$product->get_permalink()
);
$button_text = 'Reserve Item';
$link = get_bloginfo( 'url' ) . '/contact' . $product_link_params;
echo '<p class="zpd-wc-reserve-item-button">';
echo do_shortcode('<a href="'.$link.'" class="button addtocartbutton">' . $button_text . '</a>');
echo '</p>';
}
add_action( 'woocommerce_after_shop_loop_item','zpd_replace_wc_add_to_cart_button' );
add_action( 'woocommerce_single_product_summary','zpd_replace_wc_add_to_cart_button' );
@meximan
Copy link

meximan commented Jan 11, 2024

How can we edit the code to make the button "Reserve Item" go to the actual product page rather than contact?

@meximan
Copy link

meximan commented Jan 11, 2024

Is there also a way to make an exception to the out of stock code below that I'm using to show an item that's out of stock?

/*

  • replace read more buttons for out of stock items in woo 2.5.1

**/

if (!function_exists('woocommerce_template_loop_add_to_cart')) {

function woocommerce_template_loop_add_to_cart( $args = array() ) {

global $product;

if (!$product->is_in_stock()) {

echo 'Out of Stock';

}

else

{

$defaults = array(

'quantity' => 1,

'class' => implode( ' ', array_filter( array(

'button',

'product_type_' . $product->product_type,

$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',

$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : ''

) ) )

);

$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );

wc_get_template( 'loop/add-to-cart.php', $args );

}

}

}

@DeveloperWil
Copy link
Author

Is there also a way to make an exception to the out of stock code below that I'm using to show an item that's out of stock?

/*

  • replace read more buttons for out of stock items in woo 2.5.1

**/

if (!function_exists('woocommerce_template_loop_add_to_cart')) {

function woocommerce_template_loop_add_to_cart( $args = array() ) {

global $product;

if (!$product->is_in_stock()) {

echo 'Out of Stock';

}

else

{

$defaults = array(

'quantity' => 1,

'class' => implode( ' ', array_filter( array(

'button',

'product_type_' . $product->product_type,

$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',

$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : ''

) ) )

);

$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );

wc_get_template( 'loop/add-to-cart.php', $args );

}

}

}

On line 37 replace .'/contact' and the rest of the line with
. $product->get_permalink();

@DeveloperWil
Copy link
Author

Is there also a way to make an exception to the out of stock code below that I'm using to show an item that's out of stock?

/*

  • replace read more buttons for out of stock items in woo 2.5.1

**/

if (!function_exists('woocommerce_template_loop_add_to_cart')) {

function woocommerce_template_loop_add_to_cart( $args = array() ) {

global $product;

if (!$product->is_in_stock()) {

echo 'Out of Stock';

}

else

{

$defaults = array(

'quantity' => 1,

'class' => implode( ' ', array_filter( array(

'button',

'product_type_' . $product->product_type,

$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',

$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : ''

) ) )

);

$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );

wc_get_template( 'loop/add-to-cart.php', $args );

}

}

}

Yep, create an array of exempt products IDs like $exempt_products = [109, 596, 700, 1014];

Then modify your line: if (!$product->is_in_stock()) {
to: if (!$product->is_in_stock() && ( !in_array( $product->get_id(), $exempt_products ) ) ) {

That should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment