• Resolved mihaela.g

    (@mihaelag)


    Hello wordpress users,

    I am trying to use the woocommerce plugin and I have the following problem:

    On a certain webpage I display products from a specific category (by using the “product_category” shortcode). Each product has an “add to cart” button next to it. After pressing this button, the page is refreshed, the product is added to the shopping cart, but no message is displayed to notify me of the successful addition. On the other hand, if I now navigate to the shopping cart page, the message will be displayed there.

    It feels like the message was pushed into some sort of queue, but my page that displays products from a category is not picking up that message. Only when I reach the shopping cart page the message is retrieved, displayed and the message queue is empty afterwards (I don’t see it anymore if I navigate to other pages).
    (I’m not wordpress or woocommerce savvy, so this analogy with the queue is only a nice way to explain the behavior I encounter).

    Can someone suggest a way to have the success message displayed on the right page ?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter mihaela.g

    (@mihaelag)

    Ok, I figured this out on my own and in case someone else stumbles upon this problem, here’s the..

    Short answer:
    In includes/wc-notice-functions.php, after function wc_print_notices() you see this function being attached to some hooks, namely:

    add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
    add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );

    Add your own hook here. In my case, I added:
    add_action( 'woocommerce_before_my_page, 'wc_print_notices', 10 );

    Now go to your page, where you want the messages displayed, and add

    <?php
    do_action( 'woocommerce_before_my_page_page' );
    ?>

    Mind that there will be some styling tricks to do here, so just use something like Firebug to inspect differences between the notices displayed on your page and those on a default woocommerce page.
    For example, I had to insert the above php code inside a <div> whose class had to be woocommerce to see it stylized like woocommerce intended notices to look like.

    Long Answer:
    Notices are added as session variables and, after being read and displayed, they are deleted. Here’s how this happens:

    When pressing the Add to cart button on a page displaying a catalog with some of your products, function add_to_cart_action from includes/class-wc-form-handler.php is executed. This function calls the wc_add_to_cart_message, which calls wc_add_notice, which appends notification messages to the session by means of WC()->session->set(params). These messages are read, displayed and then deleted by the function mentioned in the short answer section.

    I hope someone else will also benefit from my long quest.

    Thread Starter mihaela.g

    (@mihaelag)

    This issue can now be considered solved

    Hello, I want to display some notification when you click add to cart but you didnt specify a product quantity. Is this possible?

    Thnaks

    My solution to this was to change the “add to cart” URL using this block of code in functions.php :

    
    	add_filter( 'woocommerce_product_add_to_cart_url', 'dtbaker_woocommerce_product_add_to_cart_url', 10, 2 );
    	function dtbaker_woocommerce_product_add_to_cart_url($url, $obj){
    		if(isset($obj->id) && $obj->id > 0){
    			$url = add_query_arg(array(
    				'add-to-cart'=>$obj->id,
    			),get_permalink($obj->id));
    		}
    		return $url;
    	}
    

    so now when [recent_products] woocommerce shortcode is on the home page, people click “add to cart” and it takes you to the product page AND adds the product to the cart.

    this takes care of all the css styling because all the woocommerce classes are already added to product pages.

    I’ve encountered this aswell and i find the above mentioned solutions lacking.
    First add ‘woocommerce’ as a class to all the pages of your site that woocommerce does not recognise as its own to ensure that the notices are styled consistently wherever they appear by adding this to your functions.php:

    add_filter('body_class', 'append_woo_classes');
    function append_woo_classes($classes){
    if ( ! is_woocommerce()) {
    $classes[] = 'woocommerce woomanual';
    };
    return $classes;
    };

    The “woomanual” class is so we know the class was added manually just in case we need to differentiate from woocommerces native pages.

    Second, either add:

    <?php wc_print_notices(); ?>

    to your templates or somehow hook the function to the beginning of your content and unhook the original before shop and before single product. It will still execute twice before the cart, but that will not be and issue as the first execution clears the queue. Something like this in functions.php:

    remove_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
    remove_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );
    add_action( 'YOUR_HOOK_HERE', 'wc_print_notices', 10 );

    Does anybody know how to unhook wc_print_notices from the cart page aswell?

    Nevermind, its in the cart template.. Why, woocommerce, why? :))

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Woocommerce – Successfully added to cart message not displayed’ is closed to new replies.