Thank you for your answer, Mike. I’m not really a PHP developer so I try to go by the documentation as much as I’m able but that’s not much.
There’s an example in your link: if( has_term( ‘jazz’, ‘genre’ ) )
According to that I’m trying:
function wc_custom_add_to_cart_message() {
global $woocommerce;
if (has_term('bebe', 'product_cat')) {
$message = '<a href="carrito/" class="button wc-forwards" style="margin: 2px 5px;float: left;background: #CA5C5F;">Ver carrito</a><a href="http://127.0.0.1/2016/ab0516" class="button wc-forwards" style="margin: 3px 5px; float: left;">Seguir comprando</a>';
} else {
$message = "<span>nope</span>";
}
return $message;
}
add_filter('wc_add_to_cart_message', 'wc_custom_add_to_cart_message');
And it always returns “nope”, when the product it’s on the category called “bebe”… Maybe it’s not getting the product at all? I’m doing this in the theme’s functions.php probably I’m missing something, but I don’t know exactly what.
This function checks if a POST has the term. It doesn’t affect you if you’re on a product category archive.
You want https://codex.wordpress.org/Function_Reference/is_tax
I think has_term actually works in the product page, the problem seems to be in other place. For example, this works:
function test() {
global $post;
if (has_term('tuc-tuc', 'product_tag')) {
echo "Bingo!";
} else {
echo "<h1>I shouldn't see this...</h1>";
}
}
add_filter('woocommerce_before_main_content', 'test');
But this doesn’t:
function wc_custom_add_to_cart_message() {
global $woocommerce;
if (has_term('tuc-tuc', 'product_tag')) {
$message = '<h1>Bingo!</h1>';
} else {
$message = "<h1>I shouldn't see this...</h1>";
}
return $message;
}
add_filter('wc_add_to_cart_message', 'wc_custom_add_to_cart_message');
I’m honestly clueless…
has_term uses the global $post if you don’t declare one. Your code is likely just too early to work if that makes sense?
It doesn’t really matter if I use the global $post or not, it seems that I’m not able at all to modify the woocommmerce message conditionally. I tried editing it in wc-cart-functions.php without luck…
Essentially, what I’m trying to archieve is customizing the “Product succesfully added” message with two buttons: “View cart” and “Continue shoping” (with link to product category not general “shop”).
You must be making some mistake…
The wc_add_to_cart_message filter does work. Just tried it. I simply replaced the entire string
apply_filters( 'wc_add_to_cart_message', $message, $product_id )
GIVES you the product_id. And the message. So no need to use globals.
Excuse my igorance, I’m learning here. Do you think I could use https://developer.wordpress.org/reference/functions/get_the_terms/
Like $product_term = get_the_terms( $product_id, 'product_cat');
in my function?
Yes but has_term is better 🙂
I’d like to understand this, why this doesn’t work then?
function wc_custom_add_to_cart_message() {
if ( has_term( 'tuc-tuc', 'product_tag', $product_id ) ) {
$message = '<h1>Bingo!</h1>';
} else {
$message = "<h1>I shouldn't see this...</h1>";
}
return $message;
}
add_filter('wc_add_to_cart_message', 'wc_custom_add_to_cart_message' );
I mentioned get_the_terms because I’m clueless and I’m kind of browsing all the available documentation in search of ideas, but I don’t really know what I’m missing…
Because you’re not telling the filter what product ID is.
add_filter('wc_add_to_cart_message', 'wc_custom_add_to_cart_message', 10, 2 );
^ pass 2 args in.
function wc_custom_add_to_cart_message( $message, $product_id ) {
^ assign to variables.
function wc_custom_add_to_cart_message( $message, $product_id ) {
if ( has_term( 'tuc-tuc', 'product_tag', $product_id ) ) {
$message = '<h1>Bingo!</h1>';
} else {
$message = "<h1>I shouldn't see this...</h1>";
}
return $message;
}
add_filter('wc_add_to_cart_message', 'wc_custom_add_to_cart_message', 10, 2 );
It works! Thank you man, I’ve just learnt a lot. I’ve been reading about hooks, priority, arguments, it’s really great the possibilities now. Thank you!