Created
February 21, 2016 18:28
-
-
Save themepaint/7c1bd367c3d4370f20e7 to your computer and use it in GitHub Desktop.
WooCommerce – Check if product is already in cart
This file contains hidden or 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
<?php | |
/** | |
* 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() { | |
foreach( WC()->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( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); | |
function woo_archive_custom_cart_button_text() { | |
foreach( WC()->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