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 mtx-z/90f7c35ff8714bdc533b5e7d5fbbe807 to your computer and use it in GitHub Desktop.
Save mtx-z/90f7c35ff8714bdc533b5e7d5fbbe807 to your computer and use it in GitHub Desktop.
Update Woocommerce "add to cart" button HTML template from shop and product listing pages - override product loop add to cart button template
<?php
/**
* @snippet Change WooCommerce ‘Add to Cart’ button dispalyed in product listing page (home, shop, product lists)
* There is no overridable template file for this particular button displayed for product items add to cart button in product loops
* This hook allows to update the text or HTML of this button
* @source https://gist.github.com/mtx-z/90f7c35ff8714bdc533b5e7d5fbbe807
* @compatible WC 6.3.1
*/
add_filter('woocommerce_loop_add_to_cart_link', 'tx_woo_custom_list_add_to_cart_button', 10, 2);
function tx_woo_custom_list_add_to_cart_button($button, $product)
{
// EXAMPLE: change WooCommerce 'Add To Cart' button to 'View Product' link for product from ID "2"
// you could create condition based on $product taxonomie terms (product categories), status, meta...
if ($product->get_id() === 2) {
return '<a class="button" href="' . $product->get_permalink() . '">View Product</a>';
}
// you can also edit the $button variable, that contains the HTML - or return it without modification
return $button;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment