0

I am currently making an e-shop on woocommerce. On my homepage there is products slider with the add-to-cart button under each product. If I click on the button, product is added to cart successfully, but without any message.

While surfing the Internet, I've found out, that messages can be added on shop page, product category page and product tag page (in this article). According to that article, I should use filter/hook to catch the add-to-cart event and display message on main page.

I have tried this:

    add_filter( 'woocommerce_add_to_cart_message', 'custom_add_to_cart_message' );
    function custom_add_to_cart_message() {
        global $woocommerce;
        // Output success messages
        if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
            $return_to  = get_permalink(woocommerce_get_page_id('shop'));
            $message    = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
        else :
            $message    = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
    endif;
        return $message;
}

but nothing happens. Can anyone help me?

4

Solution was as simple as it should be: I've just added this piece of code into my main page .php file:

do_action( 'woocommerce_before_single_product' );
2

This article will help you in this issue https://docs.woocommerce.com/document/woocommerce-cart-notices/

Or you can put below code in functions.php to get success message

add_filter( 'woocommerce_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
    global $woocommerce;
    // Output success messages
    if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
    $return_to = get_permalink(woocommerce_get_page_id('shop'));

    $message = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );

    else :
        $message = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );

    endif;
    return $message;
}
function your_woo_ajax_solution( $translation, $text, $domain ) {
if ( $domain == 'woocommerce' ) { // your domain name
   if ( $text == 'View Cart' ) { // current text that shows
       $translation = 'Product successfully added to your cart.'; // The text that you would like to show
   }
}
return $translation;
}
add_filter( 'gettext', 'your_woo_ajax_solution', 10, 3 ); 
0

Copy this code and paste it to your theme page.php or singlepage.php

do_action('woocommerce_before_single_product');
  • Note: This solution will print out everything that is hooked into the woocommerce_before_single_product hook. I really don't suggest this approach. – Sillo Apr 19 '18 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.