Created
November 16, 2020 03:25
-
-
Save DeveloperWil/20f16e4e6af000b2b98258db98b33cd1 to your computer and use it in GitHub Desktop.
WooCommerce: Display "Already in cart - add again?" for Add to Cart button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Change "Add to cart" button text on single product page | |
* | |
* @author Wil Brown zeropointdevelopment.com | |
* @return string|void | |
*/ | |
function zpd_wc_add_to_cart_again_button() { | |
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'); | |
} | |
add_filter( 'woocommerce_product_single_add_to_cart_text', 'zpd_wc_add_to_cart_again_button' ); | |
/** | |
* Change "Add to cart" button text in product archives | |
* | |
* @author Wil Brown zeropointdevelopment.com | |
* @return string|void | |
*/ | |
function zpd_wc_archive_add_to_cart_again_button() { | |
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'); | |
} | |
add_filter( 'add_to_cart_text', 'zpd_wc_archive_add_to_cart_again_button' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment