• Resolved linux4me2

    (@linux4me2)


    I’m trying to change the added to cart message on one page only by adding a bit of code to the functions.php in my child theme. The following code works fine to change the added to cart message for specific products:

    add_filter( 'wc_add_to_cart_message', 'add_to_cart_change', 10, 2 );
     function add_to_cart_change($message, $product_id) {
      global $woocommerce;
      $gotostep = 0;
      $products = array(1=>array(2679,2680,2681,2682));
      foreach ($products as $key=>$value) {
       if (in_array($product_id, $value)) {
        $gotostep = $key + 1;
       }
      }
      if ( $gotostep != 0 ) {
       $message = __('Product successfully added to your cart.', 'woocommerce') . ' <a href="' . $gotostep . '" class="real-amp-step-link">Go to Step ' . $gotostep . '</a>';
      }
      return $message;
     }

    I want to add is_page() in there somewhere to limit it only to one page; however, is_page() always returns false if I put it in the function, or wrap the add_filter in a conditional.

    I tried putting is_page() into wp_enqueue_scripts, where it returns true, and then calling:

    add_filter( 'wc_add_to_cart_message', 'add_to_cart_change', 10, 2 );

    but it didn’t work either.

    How would I use is_page() to conditionally change the added to cart message?

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

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

    (@mikejolley)

    is_page is not available until later, after it’s added to cart. So you cannot use this function here.

    Thread Starter linux4me2

    (@linux4me2)

    Thanks Mike, that’s what I figured. I think what I’ll do is create a custom page template for those pages and then use some jQuery to determine if the View Cart button (class wc-forward) is present. If so, I’ll know the customer added an item to the cart and I can run my code to add a message.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Change Added to Cart Message for Specific Products on One Page Only?’ is closed to new replies.