• Resolved doffine

    (@doffine)


    Hey there,

    we just noted a problem with the function wc_add_notice() on the checkout page.
    This has worked for a long time – but now the message isn’t displayed anymore.

    If we use wc_add_notice('Some error information text','error') on the checkout page it triggers the error and makes sure that the checkout isn’t proceeded (“There are some issues…”) – so far so good. But it simply doesn’t display the red message “Some error information text” anymore.

    btw: In the cart both wc_add_notice() and wc_print_notice() work. But in the checkout wc_add_notice() only triggers the error/stops the checkout process and wc_print_notice() only prints the message.

    More precisely we use wc_add_notice() to implement a minimum order amount on the action “woocommerce_check_cart_items” as shown at https://stackoverflow.com/questions/55038477/set-a-minimum-order-amount-in-woocommerce

    Here is our complete code – it always displayed the message on both cart and checkout page. Now the message is only shown on the cart but no longer on the checkout page:

    add_action('woocommerce_check_cart_items', 'set_min_total_by_country');
    function set_min_total_by_country() {
    	
        if(WC()->customer->shipping_country == "DE") { $minimum_cart_total = 30; }
        else { $minimum_cart_total = 70; }
    
    	if(is_cart() || is_checkout()) {
    		if(WC()->cart->subtotal < $minimum_cart_total) {
    			wc_add_notice( 
    				('Sie haben Ihren Mindestbestellwert von <b>'.number_format($minimum_cart_total,2,',','').' '.
                     get_woocommerce_currency_symbol(get_option('woocommerce_currency')).'</b> noch nicht erreicht. Ihr aktueller Bestellwert beträgt <b>'.
                     number_format(WC()->cart->subtotal,2,',','').' '.get_woocommerce_currency_symbol(get_option('woocommerce_currency'))).'</b>.','error');
            }
        }
    }

    Has anyone an idea why the error message isn’t displayed in the checkout page anymore when you use wc_add_notice()?

    • This topic was modified 4 years, 3 months ago by doffine.
    • This topic was modified 4 years, 3 months ago by Jan Dembowski.
Viewing 14 replies - 1 through 14 (of 14 total)
  • Hi there! If I copy/paste your exact code, I get the notification on the cart page:

    Sie haben Ihren Mindestbestellwert von 70,00 € noch nicht erreicht. Ihr aktueller Bestellwert beträgt 10,00 €.

    Screenshot: https://d.pr/i/UosZ7K

    When I proceed to checkout, I get this notification:

    There are some issues with the items in your cart. Please go back to the cart page and resolve these issues before checking out.

    Screenshot: https://d.pr/i/JYUuHa

    It’s the ‘error’ in the notification that blocks the checkout.

    If you’d like to customize that checkout message, you will want to translate that string specifically, or, as a bonus, you could send the checkout error to a custom page, as outlined here: https://gist.github.com/bekarice/99d479e6c91880b3b80a21a0b3415b7f#gistcomment-3106188

    • This reply was modified 4 years, 3 months ago by NickGreen.
    Thread Starter doffine

    (@doffine)

    Hey @nickirongate,

    thanks for coming back to us!
    What you discovered is exactly the problem. We want the message…

    Sie haben Ihren Mindestbestellwert von 70,00 € noch nicht erreicht. Ihr aktueller Bestellwert beträgt 10,00 €.

    …to be shown additionally on the checkout page, too – not only on the cart page.

    The wc_add_notice() is performed on both pages but the message is shown only on the cart page. Hasn’t this been the case earlier, that wc_add_notice() messages are printed out on the checkout page, too?

    We noted that using wc_print_notice() it is possible to show the message on the checkout page. But if we use ONLY wc_print_notice() (although with an error paramter set) on the checkout page the checkout isn’t blocked. But it must be blocked. So the only possibility we can use at this moment seems to be to perform both functions consecutively on the checkout page:

    wc_add_notice( 
    				('Sie haben Ihren Mindestbestellwert von <b>'.number_format($minimum_cart_total,2,',','').' '.
                     get_woocommerce_currency_symbol(get_option('woocommerce_currency')).'</b> noch nicht erreicht. Ihr aktueller Bestellwert beträgt <b>'.
                     number_format(WC()->cart->subtotal,2,',','').' '.get_woocommerce_currency_symbol(get_option('woocommerce_currency'))).'</b>.','error');

    AND

    wc_print_notice( 
    				('Sie haben Ihren Mindestbestellwert von <b>'.number_format($minimum_cart_total,2,',','').' '.
                     get_woocommerce_currency_symbol(get_option('woocommerce_currency')).'</b> noch nicht erreicht. Ihr aktueller Bestellwert beträgt <b>'.
                     number_format(WC()->cart->subtotal,2,',','').' '.get_woocommerce_currency_symbol(get_option('woocommerce_currency'))).'</b>.','error');

    Where the first function blocks the checkout and the second prints out the message.

    This seems to be a little bit odd to us.

    Hi Doffine,

    I reply to your problem because I have the same: on the Woocommerce plug-in I developed, the wc_add_notice() function worked as well before I update Woocommerce to 3.9.0 version. Since I did the update, the notices don’t display correctly on the checkout page.
    So think so the update generate the problem.

    Did you find a solution to solve it?

    If yes, I’m curious to know this solution ;).

    Thanks for your reply!

    best regards 😉

    Thread Starter doffine

    (@doffine)

    Hey @bbfunk,

    we also think the update generated the problem. Unfortunately we didn’t find a solution for this but are waiting for @nickirongate to answer our last post here.

    I guess in his first=last reply he didn’t get the problem so far. So I wrote our second reply that is unanswered so far.

    Do you think he will reply here again, or could it be necessary that you open a second thread for this?

    Many greetings,
    -doffine

    Hi again Doffine,

    I don’t know if nickirongate will answer here again, but I’d like to find a solution for this problem because it’s the last problem I have to finish my plug-in :).
    I’ll try to add notice in different ways to solve the problem, but without success…
    I wonder if I’ll try to add my own notices rather than Woocommerce notices…

    Best regards 😉

    Thread Starter doffine

    (@doffine)

    @bbfunk,

    do you see a chance in reaching out to them via a ticket or so?
    It seems like they lost interest in this issue here at wordpress.org.

    -doffine

    Hi there, I’m sorry this got dropped – we try to give good forum answers when we can, but since it’s on a volunteer basis, sometimes we have to focus our efforts in other areas.

    I was unable to get any movement on this specific issue, but since it’s duplicatable, I’d recommend you open an issue on the code repo. I did a quick search, and couldn’t locate this as an existing issue.

    You can file an issue here: https://github.com/woocommerce/woocommerce/issues/new/choose

    Hi NickGreen,

    Thanks for your reply ;).

    I didn’t find any issue on the Web too… but this is probably because the scenario in my plugin, like in the plugin of doffine I think, require to display specific notices after the checkout button clicked.

    I’ll probably post the problem on Github tomorrow.

    Best regards 😉

    @doffine

    There is probably a way to solve it. I’d like to solve it as soon as possible, but we need to search kinda to find the solution I think.

    Best regards 😉

    Thread Starter doffine

    (@doffine)

    @bbfunk,

    hey there. It’s great @nickirongate came back to us with the recommendation to file an issue on GitHub and I also think this is the best / only possibility for us to get this bug fixed.

    Would it be possible that you post here again when you got feedback on Github?

    Many greetings,
    -doffine

    Hi,

    For my case I found an alternative solution to solve my problem: if the conditions aren’t met to redirect to the payment form on the payment gateway, I simply redirect the customer to the cart page after added the custom notice:

    wc_add_notice(__('shit... a problem occurred, you need to remove a product from your cart!','my_gateway'), 'error');
    
    return $woocommerce->cart->get_cart_url();
    

    It works perfectly.

    Best regards 😉

    • This reply was modified 4 years, 2 months ago by bbfunk.
    Thread Starter doffine

    (@doffine)

    Hi @bbfunk,

    I see your approach, looks good. I had to use WC() instead of $woocommerce, else I got a

    Fatal error: Uncaught Error: Call to a member function get_cart_url() on null

    But then a “return WC()->cart->get_cart_url();” simply doesn’t do anything. It doesn’t perform a redirect or so. It just returns the URL. So I had to do

    wp_redirect(WC()->cart->get_cart_url());

    So it works as charm. It’s even better than showing the Checkout with an error message and the customer first has to click back to go on. This “click” is done now for the customer so that he can directly adjust his cart without an extra click.

    Thanks for this brilliant idea!
    -doffine

    Hello, has anyone else run into problems using wc_add_notice? I have custom code using the filter woocommerce_add_to_cart_validation to restrict the cart from adding products by categories and display a notice, which does occur but the text message itself is missing.

    My code is as follows:

    $message = __( 'Breakfast and non-breakfast items cannot be ordered at the same time.', 'woocommerce' );
    wc_add_notice( $message, 'error' );

    It doesn’t work even if I change it to wc_add_notice( 'Hello world!', 'error' ); and I’m sure it worked before I updated WC to 3.9.1. (my WordPress version is 5.3.2.)

    I see a thread has been opened on github but I don’t believe this issue is resolved. I’ll continue testing to see if it’s just my code.

    @doffine

    Hi, yes this solution works as charm and is better for user experience.

    Best regards 😉

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘wc_add_notice() doesn’t work on checkout page’ is closed to new replies.