'Put two php functions together (to change add-to-cart button in woocommerce)
I want to implement the code in this answer given here by @LoicTheAztec to change the add-to-cart button text for specific products. I've tried that code and it works perfectly by itself.
But I have an earlier implemented custom code (see below) to change the add-to-cart button text when the product is already in the cart which also works by itself, and which I want to keep. But when both codes are activated they don't work.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
function woo_custom_cart_button_text() {
global $woocommerce;
$class_to_append = "selected";
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if( get_the_ID() == $_product->id ) {
return __('✓ already selected', 'woocommerce');
}
}
return __('Add to Cart', 'woocommerce');
}
I've tried to put these two pieces of code into one function so that they work together but whatever I've tried I'm doing it wrong because it breaks my site.
This is what I want to achieve:
- The add-to-cart button text is changed to "go to checkout" for specified products (and doesn't change when item is in the cart).
- For all other products, the button text is set to "Add to Cart". When the item is in the cart, the button text is changed to "already selected".
If anyone can help me with this, that would be appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
