Skip to content

Instantly share code, notes, and snippets.

@michaeldoye
Created June 26, 2015 21:29
Show Gist options
  • Save michaeldoye/f250778750ac14bed8b0 to your computer and use it in GitHub Desktop.
Save michaeldoye/f250778750ac14bed8b0 to your computer and use it in GitHub Desktop.
Display “product already in cart” instead of “add to cart” button
/**
* Change the add to cart text on single product pages
*/
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
function woo_custom_cart_button_text() {
global $woocommerce;
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( get_the_ID() == $_product->id ) {
return __('Already in cart - Add Again?', 'woocommerce');
}
}
return __('Add to cart', 'woocommerce');
}
/**
* Change the add to cart text on product archives
*/
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );
function woo_archive_custom_cart_button_text() {
global $woocommerce;
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( get_the_ID() == $_product->id ) {
return __('Already in cart', 'woocommerce');
}
}
return __('Add to cart', 'woocommerce');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment