• Resolved lmlorca

    (@lmlorca)


    Hello! I’m trying to have a diferent message that offers “Continue Shopping” affter hitting the “add to cart button” instead of just “view” cart and I want it different for certain tags and categories, to offer different destintations in that “Continue shopping button”.

    I’m trying this but it doesn’t work

    function woocommrece_custom_add_to_cart_message() {
    global $woocommerce;
    if (has_term('jojo-maman', 'pa_marca')) {
    $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>noe</span>";
    }
    return $message;
    }
    add_filter('wc_add_to_cart_message', 'woocommrece_custom_add_to_cart_message');

    Any trick would be greatly appreciated… Thanks.

    https://wordpress.org/plugins/woocommerce/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    You are not passing the correct args to has_term. See https://codex.wordpress.org/Function_Reference/has_term

    Thread Starter lmlorca

    (@lmlorca)

    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.

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    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

    Thread Starter lmlorca

    (@lmlorca)

    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…

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    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?

    Thread Starter lmlorca

    (@lmlorca)

    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”).

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    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.

    Thread Starter lmlorca

    (@lmlorca)

    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?

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Yes but has_term is better 🙂

    Thread Starter lmlorca

    (@lmlorca)

    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…

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    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.

    Thread Starter lmlorca

    (@lmlorca)

    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!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Trying to customize wc_add_to_cart_message per taxonomy’ is closed to new replies.