Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 );
}
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->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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment