Frage

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?

War es hilfreich?

Lösung

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' );

Andere Tipps

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 ); 

woocommcerce has this as a separate function

wc_print_notices(); 

so just use that on your template pages. You don't need to echo or print it, just use it as is.

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

do_action('woocommerce_before_single_product');
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top